简体   繁体   中英

How to give particular amount of space to the views in Android?

How is the best way to set gravity for TextView and ImageButton? I would like have these in horizontal position and TextView should take 90% of the space and ImageButton 10%.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/info"/>


</LinearLayout>

You can use layout properties as wieghtSum and layout weight. You can use below code

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_wightSum="1" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_weight=".9" />

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/info"
     android:layout_weight=".1"
     />

I hope that solves your problem..

Try this..Use android:layout_weight=".3" make you textview width is 0dp like this android:layout_width="0dp"

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight=".3"
                android:text="TextView" />

            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="0dp"
                android:layout_weight=".1"
                android:layout_height="match_parent"
                android:src="@android:drawable/btn_dropdown"/>


        </LinearLayout>

Look at another option which can be possible now with a constraint layout.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">


    <Button
        android:id="@+id/btnThird"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:text="Hello"
        app:layout_constraintHorizontal_weight=".8"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/btnTwoThirds" />

    <Button
        android:id="@+id/btnTwoThirds"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Wordl"
        app:layout_constraintBottom_toBottomOf="@+id/btnThird"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintLeft_toRightOf="@+id/btnThird"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM