线性布局问题

发布于 2024-12-05 20:49:21 字数 2637 浏览 7 评论 0原文

我的应用程序布局有一个奇怪的问题。 我有一个列表视图,其中我为每个项目插入了自己的布局。 这很好用。

以下是插入的 XML 布局的一部分:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
    >
        <TextView
            android:id="@+id/label"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp" />
        <TextView
            android:id="@+id/price"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />
        <TextView
            android:id="@+id/days"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />
        <TextView
            android:id="@+id/test"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />
        <TextView
            android:id="@+id/status"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />
    </LinearLayout>

在我的活动中,我正在执行以下操作:

tv = (TextView) row.findViewById(R.id.price);
tv.setText(getString(R.string.wishlist_price) + ": " + "293.99€");

getString(R.string.wishlist_price) 在 string.xml 中定义为“Price”

现在奇怪的是: 对于上面的“293.99€”,手机上的输出如下所示:

Price: 293.99€

但是,如果我更改货币,例如更改为 22.95€,则输出如下所示:

Price:
22.95€

因此,它会在“Price:”之后添加换行符 我无法找出是什么原因造成的。

我用“2.99€”尝试过,它给出了预期的输出:

Price: 2.99€

有人知道是什么导致了示例“22.95€”中的换行符吗?

编辑: 围绕上面的线性布局有两个线性布局。也许它们会导致这种奇怪的行为:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
    <LinearLayout
        android:id="@+id/product_item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    >

I have a weired problem with the layout of my app.
I have a listview, in which I have inserted an own layout for each item.
This works nice.

Here is a part of the inserted XML layout:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
    >
        <TextView
            android:id="@+id/label"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp" />
        <TextView
            android:id="@+id/price"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />
        <TextView
            android:id="@+id/days"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />
        <TextView
            android:id="@+id/test"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />
        <TextView
            android:id="@+id/status"
            android:paddingTop="2px"
            android:paddingLeft="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />
    </LinearLayout>

In my activity I am doing following:

tv = (TextView) row.findViewById(R.id.price);
tv.setText(getString(R.string.wishlist_price) + ": " + "293.99€");

getString(R.string.wishlist_price) is defined in string.xml as "Price"

Now the strange thing:
For the above "293.99€" the output on the phone looks like this:

Price: 293.99€

But if I change the money, for example to 22.95€, the output looks like this:

Price:
22.95€

So it does a newline after "Price:"
I cant found out what is causing this.

I tried it with "2.99€", it gives the intended output:

Price: 2.99€

Does someone has an idea what is causing the newline in the example "22.95€"?

EDIT:
There are two linearlayout around the one above. Perhaps they cause this strange behavior:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
    <LinearLayout
        android:id="@+id/product_item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    >

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气尐潴 2024-12-12 20:49:21

这可能有效,也可能无效,但使用 String.format(String, Object...) 似乎很适合您正在做的事情。

首先将字符串 R.string.wishlist_price 更改为

<string name="wishlist_price">Price: %1$s</string>

,然后使用以下命令调用它:

tv = (TextView) row.findViewById(R.id.price);
tv.setText(String.format(getResources().getString(R.string.wishlist_price), price));

查看此内容以获取更多信息:使用字符串资源

This may or may not work, but using String.format(String, Object...) seems like a good fit for what you are doing.

First change your string R.string.wishlist_price to

<string name="wishlist_price">Price: %1$s</string>

and then call it using this:

tv = (TextView) row.findViewById(R.id.price);
tv.setText(String.format(getResources().getString(R.string.wishlist_price), price));

Take a look at this for more information: Using String Resources

(り薆情海 2024-12-12 20:49:21

我解决了:

我将wrap_content更改为fill_parent:

<TextView
                android:id="@+id/price"
                android:paddingTop="2dp"
                android:paddingLeft="15dp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="12dp" />

I solved it:

I changed wrap_content to fill_parent:

<TextView
                android:id="@+id/price"
                android:paddingTop="2dp"
                android:paddingLeft="15dp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="12dp" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文