[英]LinearLayout horizontally align textview to center of button inside linearlayout vertical orientation
我有一個垂直方向的LinearLayout。 它有兩個子元素-一個TextView和一個Button。 該按鈕應與父項的右/末端對齊。 並且textView必須對齊,以使兩個兄弟的中心都匹配。
約束:由於某些原因,不允許使用RelativeLayout。 允許使用ConstraintLayout。 我認為這可能會有所幫助。 (盡管我想盡可能避免增加復雜性。)
能做到嗎? 如果是,如何實現?
這是我的activity_main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/frame_layout_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"
tools:layout_height="400dp">
<View android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/green">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/left_overlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_weight="0.65"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:text="John Mayer Doe"
android:textSize="20sp"
tools:text="Title"/>
<TextView
android:id="@+id/textViewSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:autoSizeTextType="uniform"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textSize="15sp"
android:text="Alice Bob Charlie Dave Eve Frank"
tools:text="Subtitle"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_overlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_weight="0.35"
android:gravity="center_horizontal|end"
android:orientation="vertical">
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:gravity="center_horizontal"
android:textAlignment="center"
android:textColor="@color/black"
android:text="text on top"
tools:text="on top of button"/>
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="4dp"
android:textAlignment="center"
android:textColor="@color/white"
android:background="@color/colorAccent"
android:text="Click me now"
tools:text="Tap here now"/>
</LinearLayout>
</LinearLayout>
引用的colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorAccentBright">#D81B60</color>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="green">#3D9D2A</color>
<color name="red">#D80D15</color>
<color name="yellow">#B7AB4C</color>
<color name="maroon">#BB4767</color>
</resources>
MainActivity.kt
package com.example.flames
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
目標:我希望“頂部文字”與按鈕居中對齊。
我感謝您的幫助。 謝謝。
是的,您可以使用ConstraintLayout
以最低的復雜度輕松完成此操作,
這是您可以嘗試的解決方案,
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view5"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/green"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:paddingBottom="8dp"
android:text="John Mayer Doe"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/textViewSubTitle"
app:layout_constraintStart_toStartOf="@+id/view5" />
<TextView
android:id="@+id/textViewSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:autoSizeTextType="uniform"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:paddingBottom="8dp"
android:text="Alice Bob Charlie Dave Eve Frank"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="@+id/view5"
app:layout_constraintStart_toStartOf="@+id/view5" />
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:gravity="center_horizontal"
android:text="text on top"
android:textAlignment="center"
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/bottomrightbutton"
app:layout_constraintEnd_toEndOf="@+id/bottomrightbutton"
app:layout_constraintStart_toStartOf="@+id/bottomrightbutton" />
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="4dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorAccent"
android:text="Click me now"
android:textAlignment="center"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/view5"
app:layout_constraintEnd_toEndOf="@+id/view5" />
</android.support.constraint.ConstraintLayout>
是的,你可以
<LinearLayout
android:id="@+id/right_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:textAlignment="center"
android:text="text on top"/>
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:textAlignment="center"
android:textColor="@color/white"
android:background="@color/colorAccent"
android:text="Click me now"
tools:text="Tap here now"/>
</LinearLayout>
或使用約束布局
<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="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:text="text on top"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="@+id/bottomrightbutton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:background="@color/colorAccent"
android:text="Click me now"
android:textAlignment="center"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textontopofbutton"
tools:text="Tap here now" />
我對您的XML做了一個很小的修復,這應該可以工作。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/frame_layout_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"
tools:layout_height="400dp">
<View android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/green">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/left_overlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_weight="0.65"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:text="John Mayer Doe"
android:textSize="20sp"
tools:text="Title"/>
<TextView
android:id="@+id/textViewSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:autoSizeTextType="uniform"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textSize="15sp"
android:text="Alice Bob Charlie Dave Eve Frank"
tools:text="Subtitle"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_overlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_weight="0.35"
android:gravity="center_horizontal|end"
android:orientation="vertical">
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end"
android:layout_margin="4dp"
android:textAlignment="center"
android:textColor="@color/black"
android:text="text on top"
tools:text="on top of button"/>
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="4dp"
android:textAlignment="center"
android:textColor="@color/white"
android:background="@color/colorAccent"
android:text="Click me now"
tools:text="Tap here now"/>
</LinearLayout>
</LinearLayout>
祝一切順利 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.