Подтвердить что ты не робот

Платформа Android - макет и вес

Мне нужно построить макет с набором линейных макетов. Макет должен занимать определенный процент экрана. Мне нужно сделать это, чтобы иметь похожий вид на всех устройствах.

Вопрос:

У меня есть текст в верхнем правом макете (зеленый цвет). Всякий раз, когда я добавляю некоторые данные в текстовое представление, он нарушает весь макет, как показано ниже во втором изображении. Но мне нужно, чтобы данные обертывались автоматически, когда они достигли правого конца экрана/макета.

Пожалуйста, помогите мне решить эту проблему. Спасибо заранее...

Image1 - Графическое представление в соответствии с XML, показанным ниже

enter image description here

Image2 - показывает, как макет автоматически изменяется, когда я добавляю некоторые данные в textView

enter image description here

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" 
android:weightSum="100">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_weight="91"
    android:weightSum="100">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:orientation="vertical" 
                        android:layout_weight="40"
                        android:weightSum="235">

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical"  
                                                        android:gravity="center"
                                                        android:background="#ff0000"
                                                        android:layout_weight="100">

                                                                                    <TextView
                                                                                        android:id="@+id/textView1"
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="wrap_content"
                                                                                        android:textColor="#000000" />

                                                    </LinearLayout>

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical"   
                                                        android:background="#ffff00"
                                                        android:layout_weight="45">   



                                                                                    <ViewFlipper
                                                                                        android:id="@+id/view_flipper"
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="match_parent"
                                                                                        android:layout_below="@+id/tvItemName" >


                                                                                                                        <LinearLayout
                                                                                                                            android:layout_width="match_parent"
                                                                                                                            android:layout_height="match_parent"
                                                                                                                            android:orientation="vertical" >
                                                                                        </LinearLayout>
                                                                                     </ViewFlipper>



                                                    </LinearLayout>

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical"  
                                                        android:background="#ffffff"
                                                        android:layout_weight="90">   


                                                    </LinearLayout> 

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:orientation="vertical" 
                        android:layout_weight="60"
                        android:weightSum="100">

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical"   
                                                        android:layout_weight="30"
                                                        android:background="#00ab00"
                                                        android:weightSum="100"> 

                                                                                    <TextView
                                                                                        android:id="@+id/textView1"
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="fill_parent"
                                                                                        android:textColor="#000000"
                                                                                        android:text="Test data" />

                                                    </LinearLayout>

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical" 
                                                        android:background="#cd00ab" 
                                                        android:layout_weight="70"> 

                                                    </LinearLayout>

                    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:background="#ab0000"
    android:layout_weight="9">

</LinearLayout>

</LinearLayout>
4b9b3361

Ответ 1

Используйте этот XML файл. Я внес изменения для вас.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:weightSum="100" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="91"
    android:orientation="horizontal"
    android:weightSum="100" >

    <!-- Below is the first modification to layout_width -->

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="40"
        android:orientation="vertical"
        android:weightSum="235" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="100"
            android:background="#ff0000"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="45"
            android:background="#ffff00"
            android:orientation="vertical" >

            <ViewFlipper
                android:id="@+id/view_flipper"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/tvItemName" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>
            </ViewFlipper>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="90"
            android:background="#ffffff"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <!-- Below is the second modification to layout_width -->

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="60"
        android:orientation="vertical"
        android:weightSum="100" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="30"
            android:background="#00ab00"
            android:orientation="vertical"
            android:weightSum="100" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:text="Test data Test dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest data"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="70"
            android:background="#cd00ab"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="9"
    android:background="#ab0000" >
</LinearLayout>

</LinearLayout>

Мне все хорошо. проверьте снимок снимок.

Layout snapshot with little textLayout snapshot with more text

Ответ 2

Настройте layout_width или layout_height как 0dp вместо wrap_content. (Если он находится в вертикальном макете, layout_height должен быть 0dp; если он в горизонтальном макете, layout_width должен быть 0dp)

layout_weight описывает, как разделить оставшееся пространство после назначения layout_width s/layout_height.

Обратите внимание, что wrap_content не означает "сделать перенос текста"; это означает, что это измерение соответствует "предпочтительному" размеру представления.