我正在尝试创建这个界面:
但是今天我的结果是:
如您所见,我的布局存在一些问题:
- 我想为每个项目设置一个固定高度
- 有一些空白..
这是我的 grid_single.xml 的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#E9EAEA"
android:orientation="vertical"
android:padding="7dp" >
<ImageView
android:id="@+id/grid_image"
android:layout_width="wrap_content"
android:layout_height="170dp"
android:scaleType="centerCrop" >
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_linearlayout"
android:orientation="vertical" >
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="ufehiuhrieu gihtrg irth gi rtghrtg trgihtgtr"
android:textColor="#5D5D5D"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
最佳答案
您可能想尝试复合可绘制对象,它可以减少查看次数(并优化性能):
这是对布局的极端简化(为了获得最佳性能):
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="@drawable/bg_linearlayout"
android:drawableTop="@drawable/top1"
android:drawablePadding="8dp"
android:padding="7dp"
android:text="ufehiuhrieu gihtrg irth gi rtghrtg trgihtgtr"
android:textColor="#5D5D5D"
android:textSize="14sp"
android:textStyle="bold"
/>
注意 drawableTop
属性:这是复合 drawable 的说法:“将此图像放在此文本的顶部”
在 Java 代码中使用它:
Drawable drw = getResources().getDrawable(R.drawable.your_drawable_here);
// setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
myText.setCompoundDrawablesWithIntrinsicBounds(null, drw, null, null);
关于安卓 : How to simplify my layout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23982536/