椭圆大小的 Android AppWidget 布局问题

标签 android layout android-appwidget

我正在编写一个显示一些数字的应用程序小部件,每个数字前面都有标签。

布局看起来像这样:

.-----------------------------.
| Label1       |  AAAAAAAAAAA |
| LongerLabelLabel2 | BBBBBBB |
| LongLabel3    | CCCCCCCCCC  |
'-----------------------------'

有一个外部(垂直)LinearLayout,每一行是一个带有两个 TextView 的水平 LinearLayout。

问题是,如果标签和数字的宽度对于小部件而言太宽,数字 会变成椭圆形。我希望标签变成椭圆形。

比如上面的情况,会变成这样:

.-----------------------------.
| Label1    |     AAAAAAAAAAA |
| LongerLabelLabel2 | BBBB... |
| LongLabel3 |    CCCCCCCCCC  |
'-----------------------------'

而不是想要的

.---------------------------.
| Label1     |  AAAAAAAAAAA |
| LongerLabelL... | BBBBBBB |
| LongLabel3  | CCCCCCCCCC  |
'---------------------------'

我这辈子都想不出如何解决这个问题。感谢您的帮助。


我当前的布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="0dip"
    android:orientation="vertical"
    style="@style/WidgetBackground">


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
              android:id="@+id/ROW1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:paddingTop="7dip"
              android:paddingBottom="0dip"
              android:paddingRight="10dip"
              android:paddingLeft="10dip"
              android:orientation="horizontal">
        <TextView android:id="@+id/NAME1" android:singleLine="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" style="@style/Text.account" />
        <TextView android:id="@+id/BAL1"  android:singleLine="true" android:layout_width="fill_parent"  android:layout_height="wrap_content" android:gravity="right" style="@style/Text.balance" />
    </LinearLayout>

... (more similar rows in the outer layout)

</LinearLayout>

最佳答案

我怀疑你的问题部分是你的标签有 android:layout_width="wrap_content"。我根本不希望它变成椭圆形,因为有足够的空间来显示它。

我会从 LinearLayout 切换到 RelativeLayout。将您的数字“列”定义为 android:layout_alignParentRight="true"android:layout_width="wrap_content"。将标签“列”定义为 android:layout_alignParentLeft="true"android:layout_toLeftOf="..."(其中 ...是你的数字列)和 android:ellpsize="end"。这应该为数字提供它需要的所有空间并限制标签,迫使它变成椭圆形。

关于椭圆大小的 Android AppWidget 布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3360242/

相关文章:

java - 发生异常: android. os.NetworkOnMainThreadException

Android ListView 添加了两次项目

在我的华为手机上调试时,Android Studio 调试器在断点处崩溃/停止

android - 在 Android 4.0.3 中为选项菜单创建网格样式布局

java - Android 服务后台 Android Oreo

android - 在多个膨胀布局中获取对同一 View 的引用

java - JCheckBox下的JLabel

wpf - XAML ColumnDefinition 中 *(星号)的含义是什么?

android - 如何自动推进 ListView 应用程序小部件,如 Honeycomb 上的 StackView 应用程序小部件?

Android App Widget 预览风格