我想將兩個元素排成一行,一個向右,一個向左。我想從代碼執行此操作...並使用樣式。Android RelativeLayout將元素的樣式設置爲
首先,我用靜態layout.xml試了一下:這工作好:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/hello_world" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
然後我創建的樣式,
<style name="t1" parent="@android:style/TextAppearance.Large">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_alignParentLeft">true</item>
</style>
<style name="t2" parent="@android:style/TextAppearance.Large">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_alignParentRight">true</item>
</style>
,並試圖:
RelativeLayout rel = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
rel.setLayoutParams(params);
TextView t1 = new TextView(this);
t1.setText("balbla_1");
t1.setTextAppearance(this, R.style.t1);
TextView t2 = new TextView(this);
t2.setText("balbla_2");
t2.setTextAppearance(this, R.style.t2);
rel.addView(t1);
rel.addView(t2);
setContentView(rel);
但我看到元素重疊,因爲對齊的風格沒有被接受......是否有可能使用這些風格的程序?
我不記得是否有一種觀點。 setstyle方法。如果存在,你可以試試。我用我的手機發布這個答案。 –
號,。沒有setStyle ..但是這setTextAppearance接受樣式..例如我看到文本的大小已經改變..但對齊選項不被接受 – user1908375
看來你必須在視圖構造函數中傳遞樣式。所以你可以使用新的textview(context,null,style) –