自定义XML的RelativeLayout多次使用

问题描述:

我需要一些帮助,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

请检查我编辑的答案 –

您可以使用通货膨胀对于

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