问题
I'm trying to make two identical buttons with the same height, but when the text is too long (the second button) then breaks markup. How can I fix it?
xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<Button
android:text="test"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"/>
<Button
android:padding="0dp"
android:text="Arterial tension disturbance"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
回答1:
Add android:singleLine="true"
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<Button
android:singleLine="true"
android:text="test"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"/>
<Button
android:singleLine="true"
android:padding="0dp"
android:text="Arterial tension disturbance"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"/>
</LinearLayout>
回答2:
use wrap_content instead of 0dp
<Button
android:padding="0dp"
android:text="Arterial tension disturbance"
android:layout_height="100dp"
android:layout_width="wrap_content"
android:layout_weight="1"/>
回答3:
You can try this: (Edited)
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<Button
android:layout_width="fill_parent"
android:text="yourText"
android:layout_height="wrap_content"
android:layout_weight="50" />
<Button
android:layout_width="fill_parent"
android:text="yourText"
android:layout_height="wrap_content"
android:layout_weight="50" />
来源:https://stackoverflow.com/questions/20786948/android-button-layout-with-big-text-size