我有一个表单。其中有几行。每行有两个并排的文本视图。左边的textview是静态的,右边的textview是动态的。基于右侧文本视图的文本(可以是任意行数),下一行的起点必须如下所示。我正在使用relativelayout并使用xml编写。我该怎么做。Android相对布局对齐
注:我不想使用网格或表格布局由于一些限制。
我有一个表单。其中有几行。每行有两个并排的文本视图。左边的textview是静态的,右边的textview是动态的。基于右侧文本视图的文本(可以是任意行数),下一行的起点必须如下所示。我正在使用relativelayout并使用xml编写。我该怎么做。Android相对布局对齐
注:我不想使用网格或表格布局由于一些限制。
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); params.addRule(xxxxx);
我开发了适合您需求的自定义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 .../>
希望它能帮助:)
你可以试试这个方法
<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>
具有u检查我的答案 – kId 2014-09-12 06:08:18
@kaushik我知道,但我有很多领域,我对这种方式不感兴趣 – pradeep 2014-09-15 09:20:22