Android:不改变高度的百分比宽度

Yur*_*yak 2 android android-layout

我需要创建占用布局宽度70%的文本字段(线性布局).我用这个代码:

<EditText
    android:id="@+id/phone"
    android:layout_width="fill_parent"
    android:layout_weight=".7"
    android:layout_height="wrap_content"
    android:inputType="phone" >
Run Code Online (Sandbox Code Playgroud)

问题是,在这种情况下,高度也占布局高度的70% - 但我不需要改变它,只是希望它是"wrap_content".有什么好的解决方案吗?

UPD: 如果我创建一个新的水平线性布局并将我的文本字段放在其中,这会是一个很好的解决方案吗?或者更优雅的东西?

UPD(2): 我希望它看起来如下,但不使用marginLeft和marginRgiht,这将在不同的屏幕分辨率上给出不同的百分比

在此输入图像描述

Ale*_*scu 6

weightSum如果您打算使用权重,则需要设置控件的父级.所以是的,把它放在LinearLayout中并给出布局适当的参数.另外,请记住,权重仅影响EXTRA空间,因此您希望将宽度设置为0dp,因此整个可用空间被视为"额外"空间.最后,我认为使用整数来衡量体重可能会更快,但我不确定.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="4">

    <EditText
        android:id="@+id/phone"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="wrap_content"
        android:inputType="phone" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

更新: 如果你想让它居中,那么在左侧和右侧包含一些间隔视图,并使用适当的权重:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="1" >

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".15" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".7"
        android:inputType="phone" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".15" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)