2012-12-16 46 views
0

我想將兩個元素排成一行,一個向右,一個向左。我想從代碼執行此操作...並使用樣式。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); 

但我看到元素重疊,因爲對齊的風格沒有被接受......是否有可能使用這些風格的程序?

+0

我不記得是否有一種觀點。 setstyle方法。如果存在,你可以試試。我用我的手機發布這個答案。 –

+0

號,。沒有setStyle ..但是這setTextAppearance接受樣式..例如我看到文本的大小已經改變..但對齊選項不被接受 – user1908375

+0

看來你必須在視圖構造函數中傳遞樣式。所以你可以使用新的textview(context,null,style) –

回答

0

您是否嘗試線性佈局?喜歡;

<LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:text="@string/o1" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:text="@string/o2" /> 
    </LinearLayout> 
+1

但是應該帶什麼..我想得到和我在layout.xml中一樣的結果,但是需要編程。 – user1908375

1

正如javadoc的規定,setTextAppearence僅適用於一些屬性:

/** 
* Sets the text color, size, style, hint color, and highlight color 
* from the specified TextAppearance resource. 
*/ 

但你可以做這樣的事情

TextView t1 = new TextView(this); 
    t1.setText("balbla_1"); 
    t1.setTextAppearance(this, R.style.t1); 
    RelativeLayout.LayoutParams t1_params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    t1_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 


    TextView t2 = new TextView(this); 
    t2.setText("balbla_2"); 
    t2.setTextAppearance(this, R.style.t2); 
    RelativeLayout.LayoutParams t2_params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    t2_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 


    rel.addView(t1, t1_params); 
    rel.addView(t2, t2_params); 
+0

這是正確的答案。 「setTextAppearance()僅適用於某些屬性」 - 非常重要。當然,像Android中的其他許多東西一樣,完全是令人討厭的。換句話說,在調用setTextAppearance時,存儲在樣式中的任何「layout_x」參數都將被完全忽略。所以,有時樣式按預期工作,有時候不會。如何找出答案?沖刷文件。祝你今天愉快。 – rmirabelle

相關問題