こんにちは私は自分のデータモデルに基づいて動的にレイアウトを作成しようとしています。しかし、私はそれを行うのにいくつかの問題があります。私が持っている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レイアウトのみが表示されます。
どこを間違えているのかわからない。私がここでやっていることを達成することは可能ですか?前もって感謝します。