添加基于Android的浏览对准RelativeLayout的编程 [英] Android RelativeLayout alignment based on View added programmatically
问题描述
我已编程方式创建一个名为TableLayout数据表(类似于创建TableLayout编程的)。现在我需要的表编程方式添加到RelativeLayout的。我尝试以下方法:
I have programmatically created a TableLayout called DataTable (similar to Create TableLayout programatically). Now I need to programmatically add the table to a RelativeLayout. I try the following:
DataTable dataTable = new DataTable(getApplicationContext(), data);
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.rel_view);
rootLayout.addView(dataTable);
数据表显示,但对齐搞砸。我现在的问题是设置其它组件相对于数据表。
The dataTable shows but the alignment is messed up. My problem now is to set the other components in relation to dataTable.
下面我显示我想结束了一下。除了 my_table的
是dataTable的是应该去的。
Below I am showing what I want to end up with. Except, my_table
is where dataTable is supposed to go.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rel_view"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyViewActivity" >
<TableLayout
android:id="@+id/my_table"
android:layout_alignParentTop="true" />
<Spinner
android:id="@+id/my_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/my_table" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/my_table"
android:layout_toRightOf="@id/my_spinner"
android:text="@string/button_mine"
android:onClick="buttonPushed" />
</RelativeLayout>
一个想法是基本上保持XML的布局我有它的方式,然后以某种方式替换 my_table的
与dataTable中,保持 ID
和 layout_alignParentTop
字段。我不知道该怎么做,虽然。
One thought would be to basically keep the xml layout the way I have it, and then somehow replace my_table
with dataTable, keeping the id
and the layout_alignParentTop
fields. I don't know how to do that though.
另一个想法是使用
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(LayoutParams.blahblah)
但我想不出如何设置对齐。此外,同样,也没有 ID
为其他意见涉及到。
如果上面没有明确后,我的主要问题是:在一个RelativeLayout的,我怎么调整XML硬件codeD元素(按钮和微调),相对于以编程方式添加元素(TableLayout)?
If the above is not clear, my main question is this: In a RelativeLayout, how do I align xml hard-coded elements (Button and Spinner) with respect to a programmatically added element (TableLayout)?
推荐答案
您可以添加额外的项目布局(无论是的ViewGroup
或的LinearLayout
),可作为动态创建查看
你的容器。对齐微调
和按钮
这个额外的项目。而不是直接将您的视图布局的根视图,你将它添加到容器中。它应该始终保持你的XML布局文件设置对齐。例如,这一布局应始终保持你的数据表
下方的微调
:
You could add an extra item to your layout (either a ViewGroup
or a LinearLayout
) that serves as the container for the dynamically created View
you have. Align the Spinner
and Button
to this extra item. Instead of adding your view directly to the root view of the layout, you add it to the container. It should always keep the alignment you set in the xml layout file. For example, this layout should always keep your DataTable
below your Spinner
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rel_view"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyViewActivity" >
<TableLayout
android:id="@+id/my_table"
android:layout_alignParentTop="true" />
<Spinner
android:id="@+id/my_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/my_table" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/my_table"
android:layout_toRightOf="@id/my_spinner"
android:text="@string/button_mine"
android:onClick="buttonPushed" />
<LinearLayout
android:id="@+id/my_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/my_spinner"/>
</RelativeLayout>
这篇关于添加基于Android的浏览对准RelativeLayout的编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!