当我在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>
答案 0 :(得分:0)
NULL IN ('A', 'C')