Android:不改变高度的宽度百分比
我需要创建占版面宽度70%的文本字段(在线性版面中)。我使用以下代码:Android:不改变高度的宽度百分比,android,android-layout,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" > 问题是,在这种情况下,高度也占版面高度的70%—
<EditText
android:id="@+id/phone"
android:layout_width="fill_parent"
android:layout_weight=".7"
android:layout_height="wrap_content"
android:inputType="phone" >
问题是,在这种情况下,高度也占版面高度的70%——但我不需要更改它,只希望它是“包装内容”。有什么好办法吗
UPD:
如果我创建一个新的水平定向线性布局并将文本字段放在其中,这是一个好的解决方案吗?或者是更优雅的
UPD(2):
我希望它看起来像下面,但不使用marginLeft和marginrgit,这将在不同的屏幕分辨率上提供不同的百分比
为EditText提供左/右空白布局
<EditText
android:id="@+id/phone"
android:layout_width="fill_parent"
android:layout_weight=".7"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:inputType="phone" >
如果要使用权重,需要在控件的父控件上设置
weightSum
。所以,是的,把它放在一个线性布局中,给布局提供适当的参数。另外,请记住,重量只影响额外空间,因此您希望将宽度设置为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>
更新:
如果还希望将其居中,则在左侧和右侧包含一些间隔视图,并具有适当的权重:
<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>
您好,根据您的代码,如果您要设置权重,通常意味着我们不应该设置布局宽度来包装内容或填充父级。应该是零。所以应该是这样
<EditText
android:id="@+id/phone"
android:layout_width="0dip"
android:layout_weight=".7"
android:layout_height="wrap_content"
android:inputType="phone" >
否则,如果您尝试使用以下代码,则意味着您将获得与图像类似的结果
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_margin="30dip"
android:layout_height="wrap_content">
<EditText
android:id="@+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
<EditText
android:id="@+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
</LinearLayout>
如上图所示虽然可以使用线性布局权重进行求解,但也可以使用。只需添加两条具有15%和85%位置值的垂直准则,并将EditText width约束到这些准则:
no无需设置weightSum,默认为1。你总是可以给出浮点数,比如“.5”、“.9”等等……这并不能回答我的问题——它仍然不起作用。我已经试过了,效果很好。是否确实已将宽度设置为0dp并将其包含在带有宽度填充的线性布局中?只需将我的代码复制粘贴到布局中,它就应该可以工作…@GökhanBarıAker实际上,如果你不设置权重和,无论出于什么原因,它都无法工作(在Android 2.3.3上测试)。我刚刚注意到你更新了你的问题,要求字段也要居中,所以我加入了一个更新,展示了如何使用透明间隔视图来实现这个目标。谢谢,但是,同样,如果使用dp,它会在不同的屏幕分辨率上给出不同的百分比。我可以使用百分比而不是实际的像素大小来进行smth吗?百分比在xml设计文件中不受支持。我们无法使用%设置宽度或高度
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
style="@android:style/Widget.Holo.Light.EditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="1st EditText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="@+id/startGuideline"
app:layout_constraintEnd_toStartOf="@+id/endGuideline" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="2nd EditText"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintStart_toStartOf="@+id/startGuideline"
app:layout_constraintEnd_toStartOf="@+id/endGuideline" />
<android.support.constraint.Guideline
android:id="@+id/startGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.15" />
<android.support.constraint.Guideline
android:id="@+id/endGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.85" />
</android.support.constraint.ConstraintLayout>