【问题标题】:android: add view to an inflated Viewandroid:将视图添加到膨胀视图
【发布时间】:2023-03-27 21:56:02
【问题描述】:

我想从我的 res/layout 目录中扩充一个布局,并在我的活动类中添加一些视图。

我的布局xml文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_marginTop="30dip"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/reg_button"
        style="@style/simple_button"
        android:onClick="clickButton"
        android:text="@string/reg_button" />


    <!--  I WANT TO PUT CONTENT HERE ! -->

</LinearLayout>

在我的 java 代码中,我尝试扩展上述布局并添加一个带有 Button 的线性布局。

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();

    //inflate xml
    LinearLayout mainLayout = (LinearLayout)     LayoutInflater.from(getBaseContext()).inflate(R.layout.regions_search, null);


    //Create a new LinearLayout
    LinearLayout newLinear = new LinearLayout(mContext);        
    newLinear.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    //create  a new Button
    Button test = new Button(mContext);
    test.setText("My button");

    //first , I add button to the LinearLayout
    newLinear.addView(test);

    //Then, I add layout to the inflated layout
    mainLayout.addView(newLinear);


    //display
    setContentView(mainLayout);

但是 newLinear 视图(及其子视图)没有显示出来。

【问题讨论】:

  • 你想要什么?当你按下click button 然后显示My Button?
  • 我只想在调用活动时显示 MyButton。 (在某些情况下,我的按钮不会显示,这就是为什么我想在java端控制显示)

标签: android android-linearlayout layout-inflater


【解决方案1】:

在你的活动中使用它,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // display
    setContentView(R.layout.activity_main);

    Context mContext = getApplicationContext();
    LinearLayout mainLayout = (LinearLayout) findViewById(R.id.regions_search);
    // Create a new LinearLayout
    LinearLayout newLinear = new LinearLayout(mContext);
    newLinear.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    // create a new Button
    Button test = new Button(mContext);
    test.setText("My button");

    // first , I add button to the LinearLayout
    newLinear.addView(test);

    // Then, I add layout to the inflated layout
    mainLayout.addView(newLinear);
}

并在你的xml文件(activity_main.xml)中写如下图,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/regions_search"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="30dip" 
android:orientation="vertical">

<Button
    android:id="@+id/reg_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="clickButton"
    android:text="RegButton" />

<!-- I WANT TO PUT CONTENT HERE ! -->

</LinearLayout>

试试这个,它会 100% 工作。

【讨论】:

  • 在 xml 文件中 LinearLayout 标签 android:orientation="vertical" 是最重要的第一次尝试,只在布局中添加这个。如果它不起作用,请尝试所有这些代码。
  • 完美!我被说服这个任务需要通货膨胀。非常感谢!
【解决方案2】:

您可以尝试为新添加的LinearLayoutButton 添加宽度和高度参数吗?创建视图时需要这些参数。

像这样。

newLinear.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

【讨论】:

  • 谢谢。我尝试为布局和按钮添加参数,但没有显示按钮。
  • 按钮也设置相同。
  • 感谢您的帮助。感谢 Anil 的代码,我的按钮显示出来了。谢谢
【解决方案3】:

test.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1f));

评论行

setContentView(mainLayout);

【讨论】:

  • 谢谢。如果我评论“setContentView”代码,屏幕上不会出现任何内容
  • 尝试将带有重力的 setLayoutParams 添加到按钮
  • 感谢您的帮助。我已经用 Anil 的代码解决了我的问题。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多