2

我有一个带有图像和两个文本视图的相对布局(一个用于新闻标题,另一个用于日期)。我为带有标题的文本视图的宽度设置了一个硬编码值。如果我使用包装内容,它会与日期重叠。

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp" >

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dp"
    android:padding="3dp" >

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/internet"
        android:contentDescription="News" />
</LinearLayout>

<TextView
    android:id="@+id/txtTitle"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Timberlake's fourth studio album The 20/20 Experience – 2 of 2 was released on September 30, 2013."
    android:textSize="14sp" />

<TextView
    android:id="@+id/txtDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/txtTitle"
    android:layout_marginRight="5dp"
    android:gravity="right"
    android:text="23.09.2013"
    android:textColor="#C0C0C0"
    android:textSize="10sp" />

</RelativeLayout>

对于屏幕尺寸 4,7,它看起来像这样:

屏幕尺寸 4,7

但是对于屏幕尺寸 5,1,它看起来很糟糕:

屏幕尺寸 5,1

我的目标是让每个屏幕尺寸都有相同的外观。标题和日期之间的空格应始终相同。我怎样才能做到这一点?

4

1 回答 1

2

您已经在使用RelativeLayout,所以令人惊讶的是,这里有一个快速修复。TextView将您的宽度保留为match_parent,但您想要做的是将它夹在您用于约会的ImageView和之间。TextView尝试执行以下操作(请注意,我必须切换TextViews布局中的顺序才能使其工作)。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp" >

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dp"
        android:padding="3dp" >

            <ImageView
                android:id="@+id/imgIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="News" />
    </LinearLayout>

    <!-- Note: I switched txtDate and txtTitle in your layout file ONLY -->
    <!-- This will NOT change their order in your actual view           -->
    <TextView
        android:id="@+id/txtDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/txtTitle"
        android:layout_marginRight="5dp"
        android:gravity="right"
        android:text="23.09.2013"
        android:textColor="#C0C0C0"
        android:textSize="10sp" />

   <!-- This is the important part, note the android:layout_toLeftOf -->
   <!-- Also remember to change the width back to "wrap_content -->
   <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:layout_toLeftOf="@id/txtDate"  
        android:text="Timberlake's fourth studio album The 20/20 Experience – 2 of 2 was released on September 30, 2013."
        android:textSize="14sp" />

</RelativeLayout>

这应该会给你你想要的视图。

希望这可以帮助。祝你好运,编码愉快!

于 2013-11-01T14:12:16.133 回答