0

XMLファイルでビューを宣言しました。コードで定義したいのですが、設定しても何も表示されません。手伝って頂けますか?

これは私のXMLファイルです:

[...]

<View
        android:id="@+id/marco_container"
        style="@style/wrapFull"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 

[...]

そして私はそれをこのように定義します:

setContentView(R.layout.marco);
View view = (View) findViewById(R.id.marco_container);
view.inflate(getApplicationContext(), R.layout.prueba, null);

私もこの方法でそれを宣言しようとしました:

view = View.inflate(getApplicationContext(), R.layout.prueba, null);

これはprueba.xmlです

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView android:text="Prueba" style="@style/wrapContent" 
    android:layout_gravity="center"/>


</FrameLayout>
4

3 に答える 3

1

ビュービューではありません。これはカスタム ビューです。FrameLayout を拡張するカスタム ビューを作成し、コンストラクター内でビューを拡張するだけです。

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER...);
View view = inflater.inflate(getApplicationContext(), R.layout.prueba, this);

そしてあなたのメインのxmlの中に:

<com.my.path.CustomView
    android:id="@+id/marco_container"
    style="@style/wrapFull"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /> 

そしてあなたのクラスで:

setContentView(R.layout.marco);
CustomView view = (CustomView) findViewById(R.id.marco_container);
于 2013-01-09T09:03:26.770 に答える
0

このようにする必要があります:

View view2 = view.inflate(this, R.layout.prueba, null);
((ViewGroup)view).addView(view2);

もちろんview、ある種のViewGroup( RelativeLayoutLinearLayoutなど)でなければなりません。

于 2013-01-09T08:43:43.437 に答える
0
setContentView(R.layout.marco);
LinearLayout li = (LinearLayout) findViewById(R.id.marco_container);
View view = getLayoutInflater().inflate(R.layout.prueba, li, false);
li.addView(view);
于 2013-01-09T08:48:49.760 に答える