2012-12-17 61 views
1

我需要一些幫助,Android應用我做,讓我解釋一下:自定義XML的RelativeLayout多次使用

目前的解決方案:工作,但不是最佳

在我的XML,我有一個定義了TableLayout。 我以編程方式將4個視圖添加到一行(RelativeLayout),然後將此行添加到TableLayout。 這4個視圖最多可以有25行。 這工作正常。但是我在爲多個佈局設計這些樣式時遇到問題。 設置邊距,填充,以編程方式加權所有作品,但有很多不同的設備,這使得它很難維護。 (並據我所知TableRow不允許排列在右邊) 具體而言,我想將最後一個視圖對齊到屏幕的右側。

我想什麼有:

我會創建一個包含這4個視圖的佈局(可能在RelativeLayout)的XML文件。然後,我想使用我定義的XML佈局以編程方式添加這4個視圖。

我可以做某種(僞代碼上來):

TextView1 = new TextView 
TextView2 = new TextView 
TextView3 = new TextView 
TextView4 = new TextView 
//Do stuff with the textviews 
X = getXMLLayout(definedXMLlayout) 
X.view(1) = TextView1 
X.view(2) = TextView2 
X.view(3) = TextView3 
X.view(4) = TextView4 
TableLayout.addRow(X) 

這樣我可以在.xml文件樣式的RelativeLayout行並把它們下-large -land -xlarge等..地圖。

所以我的問題是:我該怎麼做?

或者,有沒有更好的方式來設計多個以編程方式創建的視圖?

編輯:我確實讀過關於此的其他SO文章,但大多數並沒有完全描述我需要什麼。我知道我可以在代碼中添加所有視圖,但是我也必須在代碼中設置它們,這是我不想要的。 (我知道的樣式文件,但是這也不是一個很好的選擇,對我來說)

GRTS

EDIT2:

嗨Shreva,

Basicly這個 「作品」,我確實有

final LayoutInflater lyInflater= (LayoutInflater) mContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

更改下面的代碼

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

而不是LinearLayout我正在使用RelativeLayout。 但由於某些原因的佈局是不正確的,這是我的xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/rl_playerCard" 
    android:layout_height="wrap_content" 
    android:background="@drawable/playercard" > 
<TextView 
     android:id="@+id/tv_min" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     style="@style/txtMin" 
     android:text="@string/str_min" /> 

    <TextView 
     android:id="@+id/tv_plus" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/tv_min" 
     style="@style/txtPlus" 
     android:text="@string/str_plus" /> 

    <TextView 
     android:id="@+id/tv_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/tv_plus" 
     style="@style/txtPlayercardGrey" 
     android:text="name" /> 

    <TextView 
     android:id="@+id/tv_score" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     style="@style/txtPlayercardGreen" 
     android:text="score" /> 
</RelativeLayout> 

填充/保證金在我styles.xml文件來完成的,也有一些文字顏色&文字大小。 3個視圖現在位於屏幕邊緣的左側,位於海誓山盟之上。 tv_score位於右側。 所以這意味着邊距/填充沒有被考慮到,無論是

android:layout_toRightOf 

任何想法,爲什麼這是怎麼回事?

GRTS

EDIT3

這些都是4周的風格我已經定義了,我有別人這是工作的罰款。當在圖形佈局中查看xml佈局時,一切看起來都很好。

<!-- playercard name --> 
    <style name="txtPlayercardGrey" parent="@android:style/Widget.TextView"> 
     <item name="android:textSize">17sp</item> 
     <item name="android:textColor">#666666</item> 
     <item name="android:layout_marginLeft">15dp</item> 
    </style> 

    <!-- playercard score --> 
    <style name="txtPlayercardGreen" parent="@android:style/Widget.TextView"> 
     <item name="android:textSize">17sp</item> 
     <item name="android:textColor">#7DC500</item> 
     <item name="android:layout_marginRight">15dp</item> 
    </style> 

    <style name="txtMin" parent="@android:style/Widget.TextView"> 
     <item name="android:textSize">25sp</item> 
     <item name="android:textColor">#ff0000</item> 
     <item name="android:paddingLeft">15dp</item> 
     <item name="android:paddingRight">15dp</item> 
     <item name="android:layout_marginLeft">15dp</item> 
     <item name="android:layout_marginRight">15dp</item> 

    </style> 

    <style name="txtPlus" parent="@android:style/Widget.TextView"> 
     <item name="android:textSize">25sp</item> 
     <item name="android:textColor">#00a62b</item> 
     <item name="android:paddingLeft">15dp</item> 
     <item name="android:paddingRight">15dp</item> 
     <item name="android:layout_marginLeft">15dp</item> 
     <item name="android:layout_marginRight">15dp</item> 
    </style> 

這裏是它的外觀圖片: http://i.stack.imgur.com/lNN05.png

這裏應該如何看起來像: http://i.stack.imgur.com/lNBYS.png

+0

這很奇怪。你可以發佈styles.xml? –

+0

後期編輯與風格。 xml – Larzzz

+0

請檢查我編輯的答案 –

回答

0

您可以使用通貨膨脹對於

final LayoutInflater lyInflater= (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
LinearLayout lLayoutRow = (LinearLayout) lyInflater.inflate(
       R.layout.table_row_layout, null); 

TextView tv1 = (TextView) lLayoutRow.findViewById(R.id.rowTv1); 
TextView tv2 = (TextView) lLayoutRow.findViewById(R.id.rowTv2); 
TextView tv3 = (TextView) lLayoutRow.findViewById(R.id.rowTv3); 
TextView tv4 = (TextView) lLayoutRow.findViewById(R.id.rowTv4); 

編輯:

我用你的styles.xml試過了你的代碼。我在主佈局中誇大了它。它給了我下面的3.2" QVGA模擬器輸出。

Screenshot

所以,問題不是因爲風格。

+1

嗨Shreya,謝謝!我會給你一個去,讓你知道它是否工作:) – Larzzz