添加多个自定义视图以编程方式布局 [英] Add multiple custom views to layout programmatically

查看:258
本文介绍了添加多个自定义视图以编程方式布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我例如有空的布局是这样的:

If I for example have empty layout like this:

layout1.xml

layout1.xml

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

</LinearLayout>

和其他一些布局是这样的:

And some other Layout like this:

layout2.xml

layout2.xml

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

    <TextView
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" 
        android:layout_weight="1"/>

</LinearLayout>

我想 layout2.xml 添加到 layout1.xml 编程。我需要有多个不同的版本 layout2.xml 这时候我想我想补充。每个人都会对TextView的和按钮不同的文本。

I would like to add layout2.xml to layout1.xml programmatically. I would need to have multiple different version layout2.xml which I would add when I want. Each one would have different text on TextView and Buttons.

在定期Android设备上查看情况下,我通常会做类似这样与 addView

In case of regular Android View I would usually do it similarly like this with addView:

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

    TextView tv1 = new TextView(this);
    tv1.setText("HELLO");
    my_linear_layout.addView(tv1);

    Button btn2 = new Button(this);
    bt2.setText("WORLD");
    my_linear_layout.addView(btn2);
}

我该怎么办呢,如果我没有这样的TextView或按钮简单的东西,如果我有我自己的XML定义视图?

How can I do it if I don't have something simple like TextView or Button, and if I have my own xml defined View?

有没有可能以某种方式把里面适配器的所有意见像是一个ListView,然后在需要的时候得到它,只需要调用 addView 对这些意见?

Would it be possible to somehow put all the views inside adapter like for a ListView and then get it when needed and just call addView on those Views?

推荐答案

您可以充气layout2.xml文件,编辑文本,并把它添加到第一个布局:

You can inflate the layout2.xml file, edit the texts, and add it to the first layout :

public class MyActivity extends Activity {

    private ViewGroup mLinearLayout; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        mLinearLayout = (ViewGroup) findViewById(R.id.linear_layout);
        addLayout("This is text 1", "This is first button", "This is second Button");
    }

    private void addLayout(String textViewText, String buttonText1, String buttonText2) {
        View layout2 = LayoutInflater.from(this).inflate(R.layout.layout2, mLinearLayout, false);

        TextView textView = (TextView) layout2.findViewById(R.id.button1);
        Button button1 = (Button) layout2.findViewById(R.id.button2); 
        Button button2 = (Button) layout2.findViewById(R.id.button3);

        textView1.setText(textViewText);
        button1.setText(buttonText1);
        button2.setText(buttonText2);

        mLinearLayout.addView(layout2);
    }
}

您可能需要更改的android:在layout2.xml根视图 WRAP_CONTENT 的layout_height

You may want to change android:layout_height of the layout2.xml root view to wrap_content.

这篇关于添加多个自定义视图以编程方式布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆