2

I need to create text field which takes 70% of layout's width (in linear layout). I use this code:

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

The problem is that in such case height also takes 70% of layout's height - but I don't need to change it, just want it to be "wrap_content". Is there any good solution?

UPD: Would it be a good solution if I create a new horizontal-orientated linear layout and place my text field inside it? Or maybe something more elegant?

UPD(2): I want it to look like below, but without using marginLeft and marginRgiht, which would give different percentage on a different screen resolution

enter image description here

4

4 に答える 4

6

You need to set weightSum on the parent of the control if you intend to use the weight. So yeah, put it in a LinearLayout and give the layout the proper parameters. Also, please remember that weight only affects the EXTRA space, so you want to set the width to 0dp, so the entire available space is considered "extra" space. Lastly, I think using integers for weight might be faster, but I'm not sure about that.

<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: If you want it centered as well, then include some spacer views to the left and to the right, with the proper weights:

<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>
于 2012-05-24T08:31:44.930 に答える
2

enter image description here Hi As per your code if you are going to set weight means normally we should not set layout width to wrap content or fill parent. it should be zero. so it should be like this.

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

else if you will try with the following code means you will get it like the image

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

Like above image

于 2012-05-24T09:06:32.407 に答える
0

give Margin Layout_left/Right padding for 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" >
于 2012-05-24T08:27:42.083 に答える
0

While it's possible to solve with LinearLayout weights it's also possible to achieve with ConstraintLayout guidelines. Just add two vertical guidelines with 15% and 85% position values and constrain EditText width to those guidelines:

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>
于 2017-11-06T08:18:57.740 に答える