前言
在写AndroidUI界面的时候,一直比较让我头疼的是GridView控件。因为在写列表的时候,我希望比如说能有3列的内容,并且这三列是平分了屏幕宽度,宽度是平分了,但是高度该怎么办?高度能用match_parent吗,好像不太好吧,那wrap_content那不行,那就用一个具体的值吧–100dp,这就更不行了!!高度设置死了,但是宽度是没有设置死,这样的话出来的界面特别难看,所以我就希望能有一个控件,它能够自己设置宽和高的比例,不管宽度怎么变,高度始终随宽度变化,这样列表的Item就会是一致大小,看起来就很好了。
实现
先给大家看一下没有使用其他控件只是一个ImageView的时候的界面效果,Item的布局文件如下:
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:scaleType="fitXY"
android:id="@+id/id_iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
然后再看一下GridView的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=