在我的布局檔案中,我有一個相對布局,在該布局中,我有一個文本視圖。 如果文本非常大,我希望它只占螢屏的60%,其余的文本將轉到新的一行。
<?xml version="1.0" encoding="utf-8"? >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
xmlns:app="http://schemas.android.com/apk/res-auto"/span>>
<RelativeLayout。
android:layout_width="wrap_content"
android:layout_height="wrap_content"。
android:layout_alignParentRight="true"。
android:layout_marginTop="5dp"。
android:layout_marginEnd="5dp"。
android:layout_marginRight="5dp"。
android:background="@drawable/rounded_corner"。
android:layout_alignParentEnd="true"/span>>
<TextView
android:id="@ id/txtSentMsg"/span>
android:layout_width="wrap_content"
android:layout_height="wrap_content"。
android:padding="10dp"。
android:text="This is sent message"
android:textColor="@color/white"。
android:textSize="18sp"。
/>
</RelativeLayout>
</RelativeLayout>/span>
這是我的布局檔案,所附的圖片可以澄清我的疑問。
如你所見,我發送的資訊占據了整個螢屏。我希望布局能向右對齊,并且最多占據螢屏的60%。
更新 :
<?xml version="1.0" encoding="utf-8"? >
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"。
xmlns:app="http://schemas.android.com/apk/res-auto"。
xmlns:tools="http://schemas.android.com/tools"。
android:animateLayoutChanges="true"。
android:layout_marginTop="4dp"。
android:padding="4dp"。
android:layout_height="wrap_content"/span>>
<TextView。
android:layout_width="0dp"/span>
android:layout_height="wrap_content"。
android:background="@drawable/rounded_corner"。
android:paddingStart="12dp"。
android:paddingEnd="12dp"。
android:paddingTop="8dp"。
android:paddingBottom="8dp"/span>
app:layout_constraintWidth_max="wrap"。
android:textColor="@color/white"。
android:textSize="16sp"。
app:layout_constraintEnd_toEndOf="parent"。
android:id="@ id/txtSentMsg"
app:layout_constraintTop_toTopOf="father"
app:layout_constraintWidth_percent="0.7"
tools:text="你好,這是一條很長的資訊,隨著我的成長會使用其他行。默認寬度是任何螢屏的80%。"
/>
</androidx.constraintlayout.widget.ConstraintLayout>/span>
現在我已經使用了這個,并且它起作用了。我給了我最大的60%的螢屏面積,但現在我可以看到,有時文本視圖占據了整個60%的面積,而其中的內容卻很少。
正如你所看到的,文字 "你怎么樣 "占了整個60%。當我關閉活動并再次啟動時,這個空間就會消失,并隨機出現在任何資訊中。
uj5u.com熱心網友回復:
如果你必須使用相對布局,那么將聊天矩形放在相對布局中,如下所示:
如果你必須使用相對布局,那么將聊天矩形放在相對布局中。
<?xml version="1.0" encoding="utf-8"? >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>>
<RelativeLayout。
android:id="@ id/chats"
android:layout_width="match_parent"。
android:layout_height="wrap_content"/span>>
<TextView。
android:id="@ id/textView3"
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
android:layout_alignParentEnd="true"。
android:text="TextView"。
android:layout_marginLeft="16dp"。
android:background="@android:color/holo_green_dark"
/>
</RelativeLayout>
<RelativeLayout
android:id="@ id/chatInp"/span>
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
android:layout_alignParentBottom="true"/span>>
<EditText
android:id="@ id/editTextTextPersonName"。
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
android:ems="10"。
android:inputType="textPersonName"。
android:text="Name"/>
</RelativeLayout>
</RelativeLayout>/span>
但我建議你使用下面的約束性布局:
<?xml version="1.0" encoding="utf-8"? >
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
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"/span>>
<TextView。
android:id="@ id/textView4"/span>
android:layout_width="0dp"/span>
android:layout_height="wrap_content"。
android:layout_marginTop="32dp"。
android:background="@android:color/holo_green_dark"
android:text="TextView"。
app:layout_constraintEnd_toEndOf="parent"。
app:layout_constraintStart_toStartOf="@ id/guideline2"
app:layout_constraintTop_toTopOf="father" />
<androidx.constraintlayout.widget.Guideline
android:id="@ id/guideline2"/span>
android:layout_width="wrap_content"
android:layout_height="wrap_content"。
android:orientation="vertical"。
app:layout_constraintGuide_percent="0.4"/span> />
<EditText
android:id="@ id/editTextPersonName2"/span>
android:layout_width="0dp"/span>
android:layout_height="wrap_content"。
android:ems="10"。
android:inputType="textPersonName"。
android:text="Name"。
app:layout_constraintBottom_toBottomOf="parent"。
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="father" />
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
我建議使用線性布局。它是最快的一種,而且你可以用我認為最簡單的方式實作你想要的東西:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
android:gravity="end"。
android:weightSum="1"/span>>
<LinearLayout
android:layout_weight="0.6"
android:layout_width="0dp"/span>
android:gravity="end"。
android:layout_height="wrap_content"/span>>
<TextView。
android:id="@ id/txtSentMsg"/span>
android:layout_width="wrap_content"
android:layout_height="wrap_content"。
android:padding="10dp"。
android:text="這是一條很長的資訊,將分兩行進行"。
android:background="@color/colorAccent"。
android:textSize="18sp"/span> />
</LinearLayout>
</LinearLayout>/span>
uj5u.com熱心網友回復:
使用constraint layout,你可以簡單地告訴任何視圖,在app:layout_constraintWidth_percent和app:layout_constraintHeight_percent的幫助下,它應該覆寫多大的寬度或高度百分比。
這里我將使用app:layout_constraintWidth_percent = 0.8,其中0.8表示80%。
<?xml version="1.0" encoding="utf-8"? >
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"。
xmlns:app="http://schemas.android.com/apk/res-auto"。
xmlns:tools="http://schemas.android.com/tools"。
android:animateLayoutChanges="true"。
android:layout_marginTop="8dp"。
android:padding="4dp"。
android:layout_height="wrap_content"/span>>
<TextView。
android:layout_width="0dp"/span>
android:layout_height="wrap_content"。
android:background="@color/design_default_color_primary"
android:paddingStart="12dp"。
android:paddingEnd="12dp"。
android:paddingTop="8dp"。
android:paddingBottom="8dp"/span>
app:layout_constraintWidth_max="wrap"。
android:textColor="@color/white"。
android:textSize="16sp"。
app:layout_constraintEnd_toEndOf="parent"。
android:id="@ id/textMessage"
app:layout_constraintTop_toTopOf="father"
app:layout_constraintWidth_percent="0.8"
tools:text="你好,這是一條很長的資訊,隨著我的成長會使用其他行。默認寬度是任何螢屏的80%。"
/>
</androidx.constraintlayout.widget.ConstraintLayout>/span>
uj5u.com熱心網友回復:
你可以使用ConstraintLayout而不是RelativeLayout,并在子TextView中添加app:layout_constraintWidth_percent="0.6"
<?xml version="1.0" encoding="utf-8"? >
<androidx.constraintlayout.widget.ConstraintLayout。
xmlns:android="http://schemas.android.com/apk/res/android"/span>
xmlns:app="http://schemas.android.com/apk/res-auto"。
xmlns:tools="http://schemas.android.com/tools"。
android:layout_width="match_parent"。
android:background="@color/white"
android:layout_height="match_parent">>
<TextView。
android:layout_width="0dp"/span>
android:layout_height="wrap_content"。
app:layout_constraintStart_toStartOf="father"
app:layout_constraintTop_toTopOf="parent"。
app:layout_constraintWidth_max="wrap"。
app:layout_constraintWidth_percent="0.7"。
android:background="#637796"。
android:padding="10dp">
android:textSize="25sp"。
tools:text="這是我的聊天資訊,占螢屏的70%"/>
</androidx.constraintlayout.widget.ConstraintLayout>/span>
uj5u.com熱心網友回復:
沒有必要只說LinearLayouts或RelativeLayouts。你可以將兩者結合起來,創建具有特定邏輯的部門。在我的例子中,你的父體是LinearLayout與RelativeLayout和LinearLayout相結合作為子視圖。正如你所看到的,我模擬了一個有2個TextViews的對話,其中一個是多線擴展到精確到螢屏的60%,而下面一個只有內容提供的長度。為了表明這兩個都能作業。我想這正是你想要的。為了更加清晰,我把左邊的空白處變成了灰色的物體,以便更好地看到40/60的百分比。另外,TextViews沒有到達邊界,因為我給了一個10dp的邊距,以便更好地看。我們使用的技術叫做layout_weight和weightSum,由LinearLayout提供。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"。
android:layout_height="wrap_content"。
android:orientation="horizontal"。
android:weightSum="10">>
<LinearLayout>
android:layout_width="0dp"/span>
android:layout_height="match_parent"。
android:background="#647678"/span>
android:layout_weight="4">>
<!--你的RelativeLayout左邊40%的螢屏留白-->>
</LinearLayout>/span>
<RelativeLayout
android:layout_width="0dp"/span>
android:layout_height="wrap_content"。
android:layout_weight="6"。
android:background="@color/white"/span>
android:orientation="vertical">>
<!--60%的螢屏文本視圖在你的LinearLayout的右邊-->
<TextView
android:id="@ id/tv1"/span>
android:layout_width="wrap_content"
android:layout_height="wrap_content"。
android:layout_alignParentEnd="true"。
android:layout_margin="10dp"。
android:background="#304FFE"。
android:padding="5dp">
android:text="How are you my love? 你還好嗎?你今天過得好嗎?"
android:textColor="#ffffff"/span> />
<TextView
android:id="@ id/tv2"。
android:layout_width="wrap_content"
android:layout_height="wrap_content"。
android:layout_below="@id/tv1"。
android:layout_alignParentEnd="true"。
android:layout_margin="10dp"。
android:background="#304FFE"。
android:padding="5dp">
android:text="你為什么不回應?"。
android:textColor="#ffffff"/span> />
</RelativeLayout>
</LinearLayout>/span>
結果:
這就是我得出的結果:
uj5u.com熱心網友回復:
你可以直接使用ConstraintLayout與一個約束寬度,并將寬度百分比約束為0.6:
app:layout_constrainedWidth="true"。
app:layout_constraintWidth_percent="0.6"。
app:layout_constraintWidth_max="wrap"/code>
<?xml version="1.0" encoding="utf-8"? >
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
xmlns:app="http://schemas.android.com/apk/res-auto"。
android:layout_width="match_parent"。
android:layout_height="wrap_content"/span>>
<TextView。
android:id="@ id/txtSentMsg"/span>
android:layout_width="wrap_content"
android:layout_height="wrap_content"。
android:layout_marginTop="5dp"。
android:layout_marginEnd="5dp"。
android:layout_marginRight="5dp"。
android:background="@color/black"。
android:padding="10dp"/span>
android:text="這是發送的資訊,最多可以到螢屏寬度的60%并繼續到下一行"。
android:textColor="@color/white"
android:textSize="18sp"。
app:layout_constrainedWidth="true"。
app:layout_constraintStart_toStartOf="father"
app:layout_constraintTop_toTopOf="parent"。
app:layout_constraintWidth_max="wrap"。
app:layout_constraintWidth_percent="0.6"/span> />
</androidx.constraintlayout.widget.ConstraintLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313054.html
標籤:





