1

我需要一些关于我正在制作的 android 应用程序的帮助,让我解释一下:

当前解决方案:工作但不是最佳的

在我的 XML 中,我有一个TableLayout定义。我以编程方式将 4 个视图添加到一行 ( RelativeLayout),然后将此行添加到TableLayout. 这 4 个视图最多可以有 25 行。这工作正常。但是我在为多种布局设计这些样式时遇到了问题。以编程方式设置边距、填充、权重都可以,但是有太多不同的设备,这使得它很难维护。(据我所知,aTableRow不允许向右对齐)具体来说,我想将最后一个视图对齐到屏幕的右侧。

我想拥有什么:

我将创建一个 XML 文件,其中包含这 4 个视图的布局(可能在 a 中RelativeLayout)。然后我想使用我定义的 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)

这样我就可以RelativeLayout在 .xml 文件中设置这些行的样式,并将它们放在-large -land -xlargeetc... 地图下。

所以我的问题是:我该怎么做?

或者,是否有更好的方法来设置多个以编程方式创建的视图的样式?

编辑:我确实阅读了有关此的其他 SO 文章,但大多数都没有准确描述我需要什么。我知道我可以在代码中添加所有视图,但是我还必须在代码中设置它们的样式,这是我不想要的。(而且我知道样式文件,但这对我来说也不是一个好选择)

总成绩

编辑2:

嗨什雷瓦,

基本上这个“有效”,我确实必须更改以下代码

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>

padding/margin 在我的 styles.xml 文件中完成,还有一些文本颜色和文本大小。3 个视图现在位于屏幕边缘的左侧,彼此重叠。tv_score 在右侧。所以这意味着边距/填充没有被考虑在内,也不是

android:layout_toRightOf

知道为什么会这样吗?

grts

编辑3

这些是我定义的 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

4

1 回答 1

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 模拟器中给了我以下输出。

截屏

所以问题不在于样式。

于 2012-12-17T11:51:29.277 回答