1

DataTableというTableLayoutをプログラムで作成しました(プログラムでTableLayoutを作成するのと同様です)。次に、プログラムでテーブルをRelativeLayoutに追加する必要があります。私は次のことを試みます:

DataTable dataTable = new DataTable(getApplicationContext(), data);
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.rel_view);
rootLayout.addView(dataTable);

dataTableが表示されますが、配置が混乱しています。私の問題は、dataTableに関連して他のコンポーネントを設定することです。

以下に、私が最終的に何をしたいのかを示します。ただし、my_tabledataTableが移動する場所です。

<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>

1つの考えは、基本的にxmlレイアウトを私が持っている方法で維持し、次に何らかの方法my_tableでdataTableに置き換えてid、フィールドとlayout_alignParentTopフィールドを維持することです。でもどうやってやるのかわからない。

別の考えは使用することです

RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(LayoutParams.blahblah)

しかし、配置を設定する方法がわかりません。また、繰り返しidますが、他のビューに関連するものはありません。

上記が明確でない場合、私の主な質問はこれです:RelativeLayoutで、プログラムで追加された要素(TableLayout)に対してxmlハードコードされた要素(ButtonおよびSpinner)をどのように整列させますか?

4

1 に答える 1

0

動的に作成されたもののコンテナとして機能するアイテム(aViewGroupまたはa )をレイアウトに追加できます。とをこの追加アイテムに合わせます。ビューをレイアウトのルートビューに直接追加する代わりに、コンテナに追加します。xmlレイアウトファイルで設定した配置を常に維持する必要があります。たとえば、このレイアウトでは常に以下を維持する必要があります:LinearLayoutViewSpinnerButtonDataTableSpinner

<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>
于 2013-03-14T23:01:49.627 に答える