2012-05-24 70 views
2

我需要创建占用布局宽度70%(线性布局)的文本字段。我用这个代码:Android:无变化高度的百分比宽度

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

的问题是,在这样的情况下,高度也需要布局的高度的70% - 但我并不需要去改变它,只是希望它成为“WRAP_CONTENT”。有没有什么好的解决方案?

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

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

enter image description here

回答

6

您需要设置weightSum在控制的父,如果你打算使用的重量。所以是的,把它放在一个LinearLayout中,并给布局适当的参数。此外,请记住,重量仅影响额外空间,因此您希望将宽度设置为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> 

UPDATE: 如果你想它为中心,以及,然后包括一些间隔意见向左和向右,用适当的权重:

<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> 
+0

没有就没有必要设置weightSum,其默认是1。你总是可以给浮动数字,如“.5”,“.9”等... –

+0

这不会给我的问题的答案 - 它仍然无法正常工作。 –

+0

我试过了,它工作得很好。您是否确定将宽度设置为0dp并将其包含在宽度为fill_parent的LinearLayout中?只需在布局中复制粘贴我的代码,它应该可以工作...... –

0

保证金Layout_left/Right Editext的填充。

<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" > 
2

enter image description here 喜按你的代码如果你要设置权重意味着通常我们不应该设置布局宽度来包装内容或填充父项。它应该是零。所以它应该是这样的。

<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> 

就像上面图片

+0

谢谢,但是,再次,如果使用dp的话,它会给不同的屏幕分辨率带来不同的百分比。我可以使用百分比而不是像素来计算实际大小吗? –

+0

百分比不会在xml设计文件中获得支持。我们无法使用%设置宽度或高度, – itsrajesh4uguys

0

虽然它可能与LinearLayout中的权重,以解决它也可以用ConstraintLayout guidelines实现。只需添加两个垂直的指导方针,15%和85%的位置值和约束的EditText宽度这些准则:

Layout Editor

<?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>