1

为了构建应用程序,我们有几个列表。列表项存在问题,这是自定义的,但非常简单。格式为:

在此处输入图像描述

这代表一个带有 2 个文本视图和一个图像视图的列表项

请注意,标题和日期实际上是在彼此下方,图像在右侧,中心垂直作为属性。图像不应位于两个文本视图之间

我将先给出 XML,然后再解释确切的问题。

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView 
            android:textSize="16dip" 
            android:id="@+id/title"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>

        <TextView 
            android:textSize="12dip" 
            android:id="@+id/date"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="5dip" >

        <ImageView
            android:id="@+id/validationStateImg"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>
</LinearLayout>

问题: 从某种意义上说,这种布局完全按照 ascii 表示形式显示所有内容。当文本变长时,无法正常工作。在文本很长但不足以占用 2 行的情况下,它只会使图像视图变得很小。

在其他情况下,它只是将 imageview 完全推离屏幕。

我需要的是,当日期或其他文本视图的长度太长时,不能换行。当然,它需要是一种可移植到各种屏幕尺寸的解决方案。

我不是 UI 艺术家,因此,如果我在某种意义上滥用布局,我表示歉意,因为它们不应该被使用。

除了帮助,也欢迎提示和提示!

4

4 回答 4

6

我认为你最好的选择是一个简单的RelativeLayout:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    >
    <ImageView
        android:id="@+id/validationStateImg"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        />
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/validationStateImg"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="16dp"
        />
    <TextView
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/validationStateImg"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/title"
        />
</RelativeLayout>

将 ImageView 对齐到父对象的右侧,让 TextViews 占据其余的宽度,但与 ImageView 的左侧对齐。

于 2013-01-26T06:41:34.077 回答
2

希望这可以帮助:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<LinearLayout
    android:layout_weight = "1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dasdfasdfasdfasdfsadfsdf dsfasdfasd asdfadsf dfasdfads asdfasdf"
        android:textSize="16dip" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dasdfasdfasdfasdfsadfsdf"
        android:textSize="12dip" />
</LinearLayout>

<RelativeLayout
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_marginRight="5dip" >

    <ImageView
        android:id="@+id/validationStateImg"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />
</RelativeLayout>

于 2013-01-26T06:18:11.473 回答
1

我认为在这种情况下你需要一个不同的布局,检查这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView 
           android:textSize="16dip" 
           android:id="@+id/title"
           android:layout_width="match_parent" 
           android:layout_height="wrap_content"/>
        <TextView 
           android:textSize="12dip" 
           android:id="@+id/date"
           android:layout_width="match_parent" 
           android:layout_height="wrap_content"/>
    </LinearLayout>
    <ImageView
       android:id="@+id/validationStateImg"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical"/>

</LinearLayout>
于 2013-01-26T06:07:19.303 回答
0

如果您不希望您的图像缩小,那么您需要为其提供其父级提供的房地产的特定比例。

具体来说,作为其父级的 LinearLayout 需要有一个 weight_sum,然后文本和图像都需要有一个 layout_weight。

重量应该加起来。重量不必是整数,layout_width 必须为 0 非常重要(并且非常反直觉)(假设他们两个并排坐着)......所以......

我正在删除下面您需要重新添加的所有额外内容,只留下重要部分....

<LinearLayout weight_sum=2 layout_height=fill_parent>
   <TextView layout_weight=2 layout_height=0dp/>
   <ImageView layout_weight=2 layout_height=0dp />
   <TextView layout_weight=2 layout_height=0dp/>
</LinearLayout>

这会将 LinearLayout 分成两半,左侧是文本(如果可以的话,它将换行),右侧是图像。您可以通过将总和设置为 3 并将边拆分为 2 和 1 来调整它,在这种情况下,文本将占屏幕的 2/3,图像占 1/3。或者将总和保留为 2,让左边为 1.3,右边为 0.7,以获得类似的效果。

于 2013-01-26T06:14:44.280 回答