安卓 : How to simplify my layout?

标签 android android-layout android-linearlayout android-relativelayout

我正在尝试创建这个界面:

enter image description here

但是今天我的结果是:

enter image description here

如您所见,我的布局存在一些问题:

  • 我想为每个项目设置一个固定高度
  • 有一些空白..

这是我的 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/

相关文章:

android - 如何避免自定义用户设置影响应用程序中的字体大小

android - 在相对布局中播放 gif 图像

android - 如何模拟自动化 Android Studio UI Espresso 测试套件的网络层响应?

android - 将 View 添加到 ScrollView,然后滚动到底部 : Which callback is needed?

android - 在长按事件上删除 LinearLayout

android - 如何使用 getResource.getIdentifier() 获取布局?

android - 无法在线性布局中的第一个图像下方设置另一个图像

Android ICS - 嵌入式 youtube 视频无法在全屏模式下工作

android - 动态 TextView 显示不正确

java - NotificationCompat v7 不适用于 Oreo 及更高版本的操作系统