我想將TextView在下面的布局中垂直居中,id text_no_items并保持布局可滾動,以防帶有 id描述的TextView很長,我們需要滾動以查看完整內容。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@ id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is a sample description about the items you will see below. This text could be very long."
android:textSize="32sp" />
<TextView
android:id="@ id/text_no_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="No items"
android:textSize="48sp" />
</LinearLayout>
該TextView的垂直居中如果我們設定父布局(的高度的LinearLayout)是match_parent并給予布局權重,如下。但在這種情況下,布局不可滾動。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@ id/description"
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is a sample description about the items you will see below. This text could be very long."
android:textSize="32sp" />
<TextView
android:id="@ id/text_no_items"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center"
android:text="No items"
android:textSize="48sp" />
</LinearLayout>
歡迎使用其他布局的建議。
更新:添加參考影像。抱歉影像質量不好

uj5u.com熱心網友回復:
使用ConstraintLayout并將描述 TextView 放入其中,NestedScrollView如下例所示。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@ id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:text="@string/sample_text"
android:textSize="32sp" />
</androidx.core.widget.NestedScrollView>
<TextView
android:id="@ id/text_no_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No items"
android:textSize="48sp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
更新
使用ScrollView作為根布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/sample_text"
android:textSize="32sp"
app:layout_constraintBottom_toTopOf="@id/text_no_items"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/text_no_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="No items"
android:textSize="48sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/description" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
這是
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/340667.html
標籤:安卓 安卓布局 android-线性布局
