【问题标题】:Add Linear-layout to Linear-layout in android Dynamically动态添加线性布局到android中的线性布局
【发布时间】:2014-11-08 07:34:33
【问题描述】:

我有这样的数据

 <LinearLayout> ===> vertical
     <LinearLayout> ===>Horizontal
       <TextView>    <TextView>  <TextView>    <TextView>
    <LinearLayout> ===>Horizontal
      <TextView>    <TextView>  <TextView>    <TextView>
    <LinearLayout> ===>Horizontal
      <TextView>    <TextView>  <TextView>    <TextView>

 <LinearLayout>

如何动态添加这些布局?

谢谢

【问题讨论】:

  • 这并不难。您需要为所有LinearLayout 设置LayoutParams 并根据您的要求添加视图。
  • 难度不大也可以创建coustem listview

标签: android textview android-linearlayout


【解决方案1】:

我将帮助您完成我完成的代码。请注意,我不会为您编码(目前仍在帮助某人)。如果您有任何问题,请随时提出。

在这里,我在onClick 中的linear layout 上添加了spinnerbutton,所有这些都是动态的:

    else if(v == btnAddChild)
    {
        LinearLayout dynamicLayout = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.weight = 1.0f;
        params.gravity = Gravity.CENTER_VERTICAL;
        dynamicLayout.setLayoutParams(params);
        dynamicLayout.setOrientation(LinearLayout.HORIZONTAL);
        int layoutId = totalDynamicChild + 1000; //dynamicLayout always have dynamic spinnser's id + 1000 to avoid same id
        dynamicLayout.setId(layoutId);

        Spinner spinner = new Spinner(this);
        spinner.setLayoutParams(params);
        spinner.setAdapter(spinChildAdapter);
        spinner.setId(totalDynamicChild);   
        spinnderIdList.add(totalDynamicChild);

        ImageButton btn = new ImageButton(this);
        btn.setImageDrawable(getResources().getDrawable(R.drawable.action_delete));
        btn.setBackgroundResource(0);
        btn.setLayoutParams(params);
        int btnId = totalDynamicChild + 2000; //btn always have dynamic spinnser's id + 2000 to avoid same id
        btn.setId(btnId);

        dynamicLayout.addView(spinner);
        dynamicLayout.addView(btn);
        parentSpinner.addView(dynamicLayout);
        totalDynamicChild++;

这就是我检索spinner's 值的方式:

    for(int i = 0; i < totalDynamicChild; i++)
    {
        Spinner s = (Spinner)findViewById(spinnderIdList.get(i));
        postParam.put("child_id[" + i + "]", "example");
    }

【讨论】:

    【解决方案2】:

    为垂直 LinearLayout 创建一个 xml,以 ScrollView 作为父级,因此当 LinearLayout 没有子级时,它将应用滚动。

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
        </LinearLayout>
    </ScrollView>
    

    垂直 LinearLayout 子级的另一个 xml,其中包含水平 LinearLayout 中的 4 个 TextView。

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    
    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    
    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    

    现在如何将项目添加到垂直线性布局:

    private LinearLayout container;
    private Context context;
    private int size=3;
    private int counter=1;
    
    context = this;
    container = (LinearLayout) findViewById(R.id.container);
    
    for (int i=0;i<size;i++){
         View view  = LayoutInflater.from(context).inflate(R.layout.row_item,null);
         TextView textView1 = (TextView) view.findViewById(R.id.textView1);
         TextView textView2 = (TextView) view.findViewById(R.id.textView2);
         TextView textView3 = (TextView) view.findViewById(R.id.textView3);
         TextView textView4 = (TextView) view.findViewById(R.id.textView4);
    
         textView1.setText(String.valueOf(counter++));
         textView2.setText(String.valueOf(counter++));
         textView3.setText(String.valueOf(counter++));
         textView4.setText(String.valueOf(counter++));
    
         view.setTag((i+1));
         view.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                  Toast.makeText(context,String.valueOf((Integer)v.getTag()),Toast.LENGTH_SHORT).show();
                }
         });
         container.addView(view);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多