2012-05-24 69 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>