Android:以編程方式設置視圖樣式這是XML:<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/LightStyle"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:clickable="true"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" /></RelativeLayout>如何以style編程方式設置屬性?
3 回答

慕勒3428872
TA貢獻1848條經驗 獲得超6個贊
從技術上講,您可以使用自定義視圖以編程方式應用樣式:
private MyRelativeLayout extends RelativeLayout { public MyRelativeLayout(Context context) { super(context, null, R.style.LightStyle); }}
一個參數構造函數是以編程方式實例化視圖時使用的構造函數。
因此,將此構造函數鏈接到帶有樣式參數的super。
RelativeLayout someLayout = new MyRelativeLayout(new ContextThemeWrapper(this,R.style.RadioButton));
或者@Dori指出:
RelativeLayout someLayout = new RelativeLayout(new ContextThemeWrapper(activity,R.style.LightStyle));

SMILET
TA貢獻1796條經驗 獲得超4個贊
更新:在回答此問題時(2012年中,API級別14-15),以編程方式設置視圖不是一種選擇(即使存在一些非常重要的變通方法),而這是在更新的API之后實現的。版本。有關詳細信息,請參閱@ Blundell的答案。
老答案:
您無法以編程方式設置視圖的樣式,但您可能會發現此線程很有用。
- 3 回答
- 0 關注
- 380 瀏覽
添加回答
舉報
0/150
提交
取消