【问题标题】:Line Up All Images With Same Size排列所有相同尺寸的图像
【发布时间】:2015-06-22 20:58:09
【问题描述】:

我有多个文本和图像视图显示在 ListView 中,图像具有不同的形状,我希望它们都具有相同的高度,并且它正在工作,但现在有这些巨大的空间出现如何修复

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
    android:id="@+id/textView1"
    android:textColor="@color/colorWhite"
    android:layout_marginStart="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="wrap_content"
    android:layout_height="35dp"/>

<TextView
    android:id="@+id/textView2"
    android:textColor="@color/colorWhite"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="wrap_content"
    android:layout_height="35dp"/>

【问题讨论】:

  • 固定文本宽度

标签: android android-layout android-listview android-imageview


【解决方案1】:

您的问题是由 textview 引起的,如果您更改 textview 内容,则大小会更改并移动您的 imageviews 。您应该设置一个固定值的宽度,例如 100dp,或者使用 layout_weight 分配比例权重,如下所示:

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
    android:id="@+id/textView1"
    android:textColor="@color/colorWhite"
    android:layout_marginStart="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="35dp"
    android:layout_height="35dp"/>

<TextView
    android:id="@+id/textView2"
    android:textColor="@color/colorWhite"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="35dp"
    android:layout_height="35dp"/>

或者这个(带权重):

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weight_sum="1">

<TextView
    android:id="@+id/textView1"
    android:textColor="@color/colorWhite"
    android:layout_marginStart="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textStyle="bold" 
    android:layout_weight="0.4"/>

<ImageView
    android:id="@+id/imageView1"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="0dp"
    android:layout_weight="0.1"    
    android:layout_height="35dp"/>

<TextView
    android:id="@+id/textView2"
    android:textColor="@color/colorWhite"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:layout_weight="0.4"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="0dp"
    android:layout_height="35dp"
    android:layout_weight="0.1"/>

【讨论】:

    猜你喜欢
    • 2020-08-15
    • 2014-12-05
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多