【问题标题】:Align android text and images in listview to be the same将listview中的android文本和图像对齐为相同
【发布时间】:2012-09-19 23:01:41
【问题描述】:

我需要一些帮助来将自定义列表视图与图像和文本对齐。

我正在尝试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center"
        android:paddingTop="20dp"
        android:layout_weight="2"
        android:text="TextView" />
</LinearLayout>

而且我认为 weight 是保持元素的比例,而不是元素的内容,但显然它关心文本视图内部的东西,所以我的列表看起来很乱。

无论文本在文本视图中多长时间,如何对齐使其相同?

所以无论右手边的文字如何,绿线都应该是垂直的直线。

Tnx

【问题讨论】:

  • 您是否尝试过使用“简单”文本视图并将图像添加为最左侧的复合可绘制对象?可绘制的边界可以(并且必须,除非您使用固有尺寸)设置为您所需的 50dp。在这种情况下,文本视图应该填充父视图,并以 c 的重力为中心。
  • 我在几个地方使用了自定义适配器,所以我没有尝试将所有内容都放在简单的文本视图中。

标签: android android-layout alignment


【解决方案1】:

基本上,你的布局应该是这样的:

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center"
    android:paddingTop="20dp"
    android:layout_weight="2"
    android:drawableLeft="@drawable/ic_launcher"
    android:text="TextView" />

android:drawableLeft="@drawable/ic_launcher" 定义了最左边的复合可绘制对象,它会很好地对齐。

【讨论】:

    【解决方案2】:

    删除android:layout_weight

    由于您定义了宽度,这将对齐您的图像。

    如果您也想对齐文本,请移除 android:gravity 或将其设置为左侧。

    您可以将android:marginRight 添加到 imageView 以获得更好的可读性。

    【讨论】:

    • Tnx,我使用了你的一些建议,并且我已经修复了 textview 的大小,以保持它与文本长度无关。
    【解决方案3】:

    使用

    android:layout_height="0dp"
    

    在您的文本视图中。它将为您的所有元素提供固定大小。在这里,权重参数共享的大小是可变的,因为它取决于文本的数量。

    【讨论】:

      【解决方案4】:

      试试这个:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
      
      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" >
      
          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="0dp"
              android:layout_height="50dp"
              android:layout_gravity="center_vertical"
              android:layout_weight="1"
              android:src="@drawable/ic_launcher" />
      
          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical"
              android:layout_weight="2"
              android:text="TextView" />
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-09
        • 2011-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-06
        • 1970-01-01
        相关资源
        最近更新 更多