如何在ParentLateout中添加LinearLayout及其子内容?

时间:2015-08-26 14:34:45

标签: java android xml android-linearlayout

当我在Android中尝试在运行时添加LinearLayout时,我添加的视图和布局未显示。

问题是当我将TextView添加到ParentLinearLayout时,它正在显示。但是,如果我添加到childLinearLayout并且当childlayout添加到parentlayout时,它没有显示任何内容。

我有ParentLinearLayout这是用xml设计的,现在我需要来自java代码的子LinearLayout及其内容。

结构:

  • ParentLinearLayout(以Xml定义)

    • Child LinearLayout(从代码中获取)
      • 的TextView
      • 按钮

Java代码:

    LinearLayout ParentLayout = (LinearLayout) findViewById(R.id.parentLayout);
    LinearLayout childLayout = new LinearLayout(MainActivity.this);
    childLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200));
    childLayout.setBackgroundColor(Color.parseColor("#0F0"));
    childLayout.setPadding(10, 10, 10, 10);
    //defined in Xml
    ParentLayout.addView(childLayout);

    TextView textView1 = new TextView(MainActivity.this);
    LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textView1.setLayoutParams(tvParams);
    textView1.setText("Offers");
    textView1.setBackgroundColor(Color.parseColor("#FF0000"));
    textView1.setPadding(0, 5, 0, 10); // in pixels (left, top, right, bottom)

    Button btnAB= new Button(MainActivity.this);
    LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(40,40); //width:40dp and height:40dp
    btnAB.setLayoutParams(btnParams);
    final int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        btnAB.setBackgroundDrawable( getResources().getDrawable(R.drawable.icon) );
    } else {
        btnAB.setBackground( getResources().getDrawable(R.drawable.icon));
    }
    btnAB.setClickable(false);

    childLayout.addView(textView1);
    childLayout.addView(btnAB);

XML:

    <RelativeLayout
        android:id="@+id/scrollViewLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:elevation="@dimen/abc_action_bar_content_inset_material"
        android:orientation="horizontal">

        <HorizontalScrollView
            android:id="@+id/horizontalScroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:requiresFadingEdge="vertical"
            android:fadingEdge="vertical"
            android:paddingTop="15dp"
            android:scrollbars="none">

            <LinearLayout
                android:id="@+id/parentLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center|bottom"
                android:orientation="horizontal">
                <!-- Here I need to add one linear layout and inside of it TextView and Button -->
            </LinearLayout>
        </HorizontalScrollView>
    </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

NULL IN ('A', 'C')