具有动态内容文本的Android布局

时间:2013-10-18 10:40:13

标签: android android-layout

我需要一个带有5个textview元素的android布局。布局应该是这样的:

DynamicLargeText1                                 DynamicLargeText2
DynamicSmallText1              DynamicSmallText2  DynamicSmallText3

由于所有textview元素都可以使其内容文本增长/缩小(动态),我需要特别注意 因为这两个元素之间有固定的空间,所以要正确定位两个元素(DynamicSmallText2和DynamicSmallText3)。例如,当DynamicSmallText2在上下文文本中增长时,id不应与DynamicSmallText3重叠,反之亦然。

我希望有些人可以提前帮助我。

Smalltext2和SmallText3在我的代码中重叠了吗?

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

<TextView
        android:id="@+id/largeText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="largeText1" />

<TextView
        android:id="@+id/smallText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="smallText1"
        android:layout_below="@id/largeText1"/>


<TextView
        android:id="@+id/largeText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="largeText2"
        android:layout_alignParentRight="true"/>

<TextView
        android:id="@+id/smallText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="largeText2"
        android:layout_alignParentRight="true"
        android:layout_below="@id/largeText2"
        android:layout_marginRight="10dp"/>

<TextView
        android:id="@+id/smallText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="smallText3"
        android:layout_alignParentRight="true"
        android:layout_below="@id/largeText2"
        android:layout_toLeftOf="@id/smallText2"/>

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

使用RelativeLayout而不是LinearLayout,然后在TextView声明中添加属性“android:layout_below =”@ + id / textView1“,例如在textView2中,这将使textView2始终保持在1以下。

答案 1 :(得分:0)

使用重量这样的概念

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:gravity="bottom|right"
        android:orientation="vertical" 
        >

        <TextView
          android:layout_marginRight="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 3" />
    </LinearLayout>

    <LinearLayout
        android:gravity="right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 5" />
    </LinearLayout>

</LinearLayout>

修改了相对布局的代码

<TextView
        android:id="@+id/smallText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/smallText2"
        android:layout_alignBottom="@+id/smallText2"
        android:layout_toLeftOf="@+id/smallText2"
        android:text="smallText3" />

答案 2 :(得分:0)

存在冲突的布局参数,这些参数不能同时满足。要获得所需结果,请从android:layout_alignParentRight="true"中删除smallText3