动态添加布局到布局中?

3

我有一个布局,需要将其插入到一些带文本块中 - 如评论日期、姓名、正文。有没有办法这样使用?

 for (int r=0; r<10;r++) {
      TextView t1=(TextView) findviewbyid(R.id.text1);
      t1="BlaBla";
      mainlayout.add(commentLayout);
 }

我希望能够得到类似于ListView的东西,但是不需要适配器等。

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:id="@+id/comm_name"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="24dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Date"
    android:id="@+id/comm_date"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_margin="20dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="60dp"
    android:layout_marginLeft="20dp"
    android:id="@+id/comm_text" />


是的,这是可能的,你对此有问题吗? - Devrim
4个回答

5
尝试在滚动视图内的线性布局中动态添加视图。试试这段代码吧。我知道它不是完美的解决方案,但希望能给你一些灵感。
public class MainActivity extends Activity {

    ScrollView scrollview;
    LinearLayout  linearLayout;
    LinearLayout.LayoutParams layoutParams;
    static int i;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollview = (ScrollView)findViewById(R.id.scrollview);
        linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
        Button button = (Button)findViewById(R.id.button);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        button.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                TextView view = new TextView(MainActivity.this);             
                view.setText(++i+" view");
                linearLayout.addView(view, layoutParams); 
            }

        });

}}

5

您可以这样做,但是您现在的方式会抛出异常,因为findViewById始终返回相同的对象,并且在第一次迭代后,t1已经有了父级。您应该在每个迭代中通过编程方式创建TextView的新实例。


1

使用以下方法

循环添加动态视图到线性布局中

TextView textView1, textView2;
LinearLayout completeLinearLayout = (LinearLayout) findViewById(R.id.ll);
for (int i = 0; i < 10; i++) {
        textView1 = new TextView(Activity.this);
        textView2 = new TextView(Activity.this);
        String name1 = "xyz";
        String name2 = "abc"
        completeLinearLayout.addView(textView1);
        completeLinearLayout.addView(textView2);
        setTextViewParam(textView1, name1);
        setTextViewParam(textView2, name2);

    }

动态设置TextView参数的方法
private void setTextViewParam(TextView tv, String text) {

    LayoutParams textLayoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    textLayoutParams.setMargins(5, 0, 0, 0);
    tv.setGravity(Gravity.LEFT);
    tv.setText(text);
    tv.setTextSize(13);
    tv.setTextColor(Color.BLACK);
    tv.setLayoutParams(textLayoutParams);

}

现在您可以根据需要和容器自定义视图。 编写愉快!


有任何问题请问我 :) - Varun Vishnoi
好的,请将您的布局与标记发布,并告诉我需要设置哪些值。我会相应地更新我的答复。 - Varun Vishnoi
你如何动态更改TextView对象,因为它总是返回相同的对象? - Varun Vishnoi

-1

所以,这段代码完全符合我的需求。 有什么问题吗??

public class InflateView extends Activity {

LinearLayout lLayout;
TextView t,f;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    lLayout = (LinearLayout)findViewById(R.id.layout1);

    for (int i=0;i<10;i++)
    {
            RelativeLayout tv = (RelativeLayout)inflater.inflate(R.layout.text, null);
            lLayout.addView(tv);
            t = (TextView) tv.findViewById(R.id.ttt);
            t.setText("aaaaaaa" + i)  ;
            f = (TextView) tv.findViewById(R.id.textView);
            f.setText(String.valueOf(i));
    }
}}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接