【问题标题】:I want to inflate an xml file into MainActivity我想将 xml 文件膨胀到 MainActivity
【发布时间】:2014-01-20 19:30:23
【问题描述】:

我有一个名为“Editt.xml”的 xml 文件,每次单击 activty_main 中名为“@+id/Button01”的按钮时,我想将 @+id/myEditText 膨胀到 MainActivity 中

“Editt.xml”是这样的:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>


<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myEditText"
    android:layout_weight="1" />   </LinearLayout>

和“activity_main”

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
    android:text="Button01"
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></Button>
<ScrollView
    android:layout_below="@id/Button01"
    android:id="@+id/scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/tableRow">



 <LinearLayout
        android:id="@+id/gamehistory"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    </LinearLayout>

</ScrollView>

【问题讨论】:

  • 使用layout inflater调用edit.xml然后使用addview方法在你的线性布局中添加视图

标签: java android xml


【解决方案1】:
View v = getInflater().inflate(Editt.xml,null);

LinearLayout linearLayout = (LinearLayout) findViewById(R.layout.game_layout);
linearLayout.addView(v);

【讨论】:

    猜你喜欢
    • 2020-04-26
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多