【问题标题】:how to set view width from font width如何从字体宽度设置视图宽度
【发布时间】:2012-12-01 06:26:14
【问题描述】:

我在水平布局中有一个 EditText 和一个 Spinner。我希望微调器的大小仅与容纳其最宽文本和 EditText 占用剩余空间所需的一样大。有没有简单的方法可以做到这一点?

例如,理想情况下,我想将微调器的宽度指定为平均字体宽度的某个倍数,在 xml 文件中声明为属性。

否则,我想我需要在运行时做一些事情,例如获取微调器中每个项目的文本宽度,并为单选按钮图标获取最大值 + 一些余量。

这是我的 xml:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal" >

  <EditText
    android:id="@+id/text"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/text" />

  <Spinner
    android:id="@+id/type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:spinnerMode="dropdown"
    android:textAppearance="@android:style/TextAppearance.Medium" />
</LinearLayout>

谢谢。

【问题讨论】:

    标签: android width spinner font-size


    【解决方案1】:

    试试这个

    <LinearLayout
        android:id="@+id/rl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal" >
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </EditText>
    
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    【讨论】:

    • 对。那应该行得通。不知道为什么我忽略了这一点。
    • 其实不行。它只为单选按钮提供足够的宽度。
    • 单选按钮从何而来?
    • 有一个区别:我已将我的声明为“下拉列表”。如果我把它拿出来,宽度就可以了。不过,我更喜欢下拉菜单。
    • 我在上面的帖子中添加了 xml。
    猜你喜欢
    • 2011-08-30
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2013-09-02
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多