【问题标题】:how to add linearlayout inside of linearlayout when application running应用程序运行时如何在线性布局内添加线性布局
【发布时间】:2013-09-30 01:52:53
【问题描述】:

我想知道,有一个线性布局并将其与 setContentView 函数一起使用。线性布局内部还有一个微调器。我想要做的是,在 /res/layout 文件夹中创建一个新布局并将其添加到我使用 setContentView 设置的布局中。

无论如何还是我需要以编程方式执行此操作?

编辑: 我想我说不出来。

我有 2 两个布局(准备好)。我将第一个布局与 setContentView 一起使用。例如,有一个按钮,如果用户单击该按钮,我想在应用程序运行时在第一个布局的底部添加第二个布局。

【问题讨论】:

    标签: android


    【解决方案1】:

    在主布局的 xml 中使用 include 最容易做到这一点

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
    
      <include layout="@layout/second" />
    
    </LinearLayout>
    

    也可以通过编程方式进行,但我认为这样更清晰。

    编辑: 要以编程方式执行此操作,请将此代码放在第一个按钮的侦听器中。

    RelativeLayout view = (RelativeLayout) findViewById(R.id.RelativeLayout1);
    
    Button b = new Button(getApplicationContext());
    b.setText("Click me too!");
    
    view.addView(b);
    

    除了创建按钮(或任何您想要的)之外,您还可以扩展预制布局。

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.second, null);
    
    view.addView(v);
    

    【讨论】:

    • 已编辑。简单地说,我想在 java 类中包含函数。
    【解决方案2】:

    我认为您不能以编程方式更改 res 文件夹。您只需要以编程方式添加任何布局。

    编辑:

    使用 findViewById 获取第二个布局的实例,并使用 setVisibility 方法控制布局的可见性。

    【讨论】:

    • 已编辑。简单地说,我想在 java 类中包含函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多