0

こんにちは私は自分のデータモデルに基づいて動的にレイアウトを作成しようとしています。しかし、私はそれを行うのにいくつかの問題があります。私が持っているxmlファイルはここにあります:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="#f9f9ff">
    <TextView
            android:layout_width="97dp"
            android:layout_height="33dp"
            android:id="@+id/textView" android:layout_alignParentLeft="true" android:layout_marginLeft="15dp"
            android:layout_alignParentTop="true" android:layout_marginTop="19dp"/>
</RelativeLayout>

これは。という名前dynamic.xmlです。そして、私は実行時にこのレイアウトを膨らませようとしています。そのために私はやっています:

    ViewGroup parent = (ViewGroup) findViewById(R.id.vertical_container); 

    //getting the TextView and EditText view.         
    LayoutInflater inflater = this.getLayoutInflater();
    View currentView = inflater.inflate(R.layout.dynamic,null);
    TextView textView = (TextView) currentView.findViewById(R.id.textView);

    //trying to change the attributes. but #fails!
    textView.setText("Setting the text from code");
    textView.setId(3);

    //setting to the view
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.dynamic, null);
    parent.addView(view);

しかし、これは機能していないようです!この線 :

textView.setText("Setting the text from code");

動作しません。エミュレータには空のテキストしか表示されませんが、テキストは表示されませんSetting the text from code。どこを間違えているのかわからない。コードに加えた変更は、エミュレーターには表示されません。

dynamic属性が指定された状態でレンダリングされたxmlレイアウトのみが表示されます。

どこを間違えているのかわからない。私がここでやっていることを達成することは可能ですか?前もって感謝します。

4

2 に答える 2

1

現在、を追加しています

view = LayoutInflater.from(getBaseContext()).inflate(R.layout.dynamic, null);parent.addView(view);

しかし、あなたはこれから使用textView.setText("Setting the text from code"); します View currentView = inflater.inflate(R.layout.dynamic,null);

currentViewをに追加していませんparent

これを試してparent.addView(currentView);

于 2013-02-06T08:35:45.843 に答える
1

結果を得るために間違ったビューを設定していると思います。レイアウトを2回インフレにする必要はありません

//getting the TextView and EditText view.         
LayoutInflater inflater = this.getLayoutInflater();
View currentView = inflater.inflate(R.layout.dynamic,null);
TextView textView = (TextView) currentView.findViewById(R.id.textView);

//trying to change the attributes. but #fails!
textView.setText("Setting the text from code");
textView.setId(3);

//Now you had customize your Textview .Now just add this customize view to your view
//setting to the view
parent.addView(currentView);
于 2013-02-06T08:44:15.957 に答える