【问题标题】:How to Add LinearLayout and its child contents Inside Parent LinearLayout?如何在父 LinearLayout 中添加 LinearLayout 及其子内容?
【发布时间】:2015-08-26 14:34:44
【问题描述】:

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

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

我有ParentLinearLayout,它是用 xml 设计的,现在我需要从 java 代码中继承 LinearLayout 及其内容。

结构:

  • ParentLinearLayout(在 Xml 中定义)

    • 子线性布局(从代码中获取)
      • 文本视图
      • 按钮

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>

【问题讨论】:

  • 请同时添加 XML。
  • 但是,到底有什么问题?
  • 当我添加这些视图时,它不会显示在 parentLayout @ssh 中
  • 你在HorizontalScrollView中使用的id为@+id/search_discover的元素在哪里?
  • 我测试了你的代码,它工作正常。为childLayout 添加背景颜色的完整十六进制值。您已输入 3 位颜色。做到 6。

标签: java android xml android-linearlayout


【解决方案1】:
    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.removeAllViews();
childLayout.addView(textView1);
childLayout.addView(btnAB);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多