-4
我需要使用視圖來設置我的表格佈局的分隔線。我可以在XML中做到這一點,但我想要以編程方式創建視圖。如何轉換Java代碼上的XML?
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aaa" />
我該怎麼做。有人能幫我嗎。提前致謝。
我需要使用視圖來設置我的表格佈局的分隔線。我可以在XML中做到這一點,但我想要以編程方式創建視圖。如何轉換Java代碼上的XML?
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aaa" />
我該怎麼做。有人能幫我嗎。提前致謝。
你試過了嗎?
View v = new View(this);
v.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 1));
v.setBackgroundColor(Color.parseColor("#aaa"));
這段代碼是創建一個imageview的並且設置其佈局PARAMS和顏色
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView iv1= new ImageView();
iv1.setLayoutParams(params);
iv1.setBackgroundColor(R.color.black); // if you define black as color in xml or any color