我正在开发一个布局文件,它有一个父 LinearLayout ,里面有两个按钮,目标是根据按钮的文本大小调整按钮对齐,例如,如果任何按钮的文本大小增加到屏幕按钮的一半应该垂直放置它们,否则应水平显示。需要帮助来解决此问题。下面是布局文件及其迄今为止尝试过的输出。
xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingVertical="0dp"
android:text="I am long text to feed this button" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Test" />
</LinearLayout>
</LinearLayout>
输出:
预期行为:
场景1:如果任何按钮的文字大小大于屏幕的一半。
场景2:如果文字大小小于屏幕的一半。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Test"
android:textSize="18sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Test"
android:textSize="18sp" />
</LinearLayout>