0

我有一个表单。其中有几行。每行有两个并排的文本视图。左边的textview是静态的,右边的textview是动态的。基于右侧文本视图的文本(可以是任意行数),下一行的起点必须如下所示。我正在使用relativelayout并使用xml编写。我该怎么做。Android相对布局对齐

enter image description here

注:我不想使用网格或表格布局由于一些限制。

+0

具有u检查我的答案 – kId 2014-09-12 06:08:18

+0

@kaushik我知道,但我有很多领域,我对这种方式不感兴趣 – pradeep 2014-09-15 09:20:22

回答

0

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); params.addRule(xxxxx);

+0

我应该添加哪条规则? – pradeep 2014-09-11 06:50:48

+0

1. params.addRule(RelativeLayout.RIGHT_OF,left_top_view_id); 2. params.addRule(RelativeLayout.RIGHT_OF,left_bottom_view_id); params.addRule(RelativeLayout.BOTTOM_OF,right_top_view_id); – Thinsky 2014-09-11 06:56:39

0

修复左侧的textview静态并从java代码中绘制剩余的项目。

+0

任何方式通过XML? – pradeep 2014-09-11 06:50:24

+0

如果你不限制硬编码值的宽度和高度,你可以使用xml,否则通过java – Shriram 2014-09-11 06:52:15

0

我开发了适合您需求的自定义RelativeLayout扩展组件。这是一个关键值线,可以从xml设置键和值。请参阅code here。样品使用在project

简要说明:

KeyValueLine是定制的RelativeLayout扩展组件。您可以使用自定义名称空间从xml设置键值和值。

要在项目中添加此视图,您应该复制xml layout到/ RES /布局文件夹(修改其外观,以满足您的要求,删除所有花式后来通过代码来进行调整),并添加

<declare-styleable name="KeyValueLine"> 
    <attr name="key" format="string" /> 
    <attr name="value" format="string" /> 
</declare-styleable> 

到你的/res/values/attrs.xml文件。然后将java implementation添加到您的项目中。瞧! - 您可以使用视图XML作为<%your-package-name%.KeyValueLine .../>

希望它能帮助:)

0

你可以试试这个方法

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

    <LinearLayout 
     android:id="@+id/firstLinearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:singleLine="true" 
      android:text="Static Text 1" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:singleLine="false" 
      android:text="Multiple line\nDynamic text 1" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/secondLinearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/firstLinearLayout" 
     android:layout_marginTop="8dp" > 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:singleLine="true" 
      android:text="Static Text 2" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:singleLine="false" 
      android:text="Multiple line\nDynamic text 2" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 
    </LinearLayout> 

</RelativeLayout>