我正在动态添加一个没有出现的TextView,问题出在哪里?
以下两行之间有区别吗:
receiptLayout.addView(order_1_confirm);
((LinearLayout) receiptLayout).addView(order_1_confirm);
好吧,它们都不起作用!
我的activity_receipt.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/receiptMainLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/image_view_rsc_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/rsc_logo"
android:src="@drawable/rsc" />
和我的ReceiptActivity.java:
public class ReceiptActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receipt);
LinearLayout receiptLayout = (LinearLayout)findViewById(R.id.receiptMainLinearLayout);
TextView order_1_confirm = new TextView(this);
order_1_confirm.setText("hello");
//receiptLayout.addView(order_1_confirm);
((LinearLayout) receiptLayout).addView(order_1_confirm);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
只需在LinearLayout
中给定android:orientation="vertical"
。
在中
LinearLayout receiptLayout = (`LinearLayout`)findViewById(R.id.receiptMainLinearLayout);
您已经将视图强制转换为LinearLayout,因此无需再次强制转换
设置TextView
的大小和颜色。也许它看起来是看不见的,因为它与背景颜色相同。
您应该始终向LinearLayout
添加方向,添加android:orientation="vertical"
会很好。