It's an Android layout question

Asked 2 years ago, Updated 2 years ago, 101 views

If you give android:layout_weight by 1 to place it like the image on the left, The ImageView ratio does not match as shown in the image on the right. TextView will also be out of proportion if the text length exceeds a certain amount.

We can fix the sizes of android:layout_width and android:layout_height, but I don't think it'll be compatible if the display size changes...

I want to make sure that ImageView or TextView is displayed within a fixed ratio, is there a way?

Or, what method do you usually use to develop ui for displays of various sizes?


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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:src="@drawable/emergency_64"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:text="text1"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="40dp"
            android:textColor="@color/colorText"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textview1"
            android:text="text2"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="40dp"
            android:textColor="@color/colorText"/>
    </LinearLayout>

android layout imageview

2022-09-20 21:44

2 Answers

ConstraintLayout Use it.


2022-09-20 21:44

When LinearLayout gives the layout_weight value, if the layout_width value is set to wrap_content, the width of the view will exceed a fixed size if it becomes larger. To solve this problem, setting the layout_width value to 0dp will solve the problem.

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/textview1"
    android:text="text2"
    android:gravity="center"
    android:layout_weight="1"
    android:textSize="40dp"
    android:textColor="@color/colorText"/>

If Orientation is Vertical, you can also set layout_height to 0dp.

When wrap_content: Picture

When 0dp: Picture


2022-09-20 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.