我有下面的布局,但是我在一行中制作的按钮在屏幕之间显示不正确:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<Spinner/>
<EditText/>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="Update notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="@+id/button" android:layout_marginLeft="-8dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" card_view:srcCompat="@android:drawable/sym_action_call"
android:id="@+id/dialBtn"/>
<ImageButton
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" card_view:srcCompat="@android:drawable/ic_menu_mylocation"
android:id="@+id/locationBtn"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" card_view:srcCompat="@android:drawable/ic_menu_send"
android:id="@+id/whasappBtn"/>
<Button
android:text="Set Alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/alarmBtn"/>
</LinearLayout>
<TextView />
<TextView />
<TextView />
<Button />
<ImageView />
</LinearLayout>
右侧的按钮 set alarm
不能正确显示,具体取决于屏幕尺寸/分辨率:
正确的布局:
错误的布局:
最佳答案
对 View 使用权重
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_weight="1"
android:text="Update notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="@+id/button" android:layout_marginLeft="-8dp"/>
<ImageButton
android:layout_weight="1.3"
android:layout_width="match_parent"
android:layout_height="wrap_content" card_view:srcCompat="@android:drawable/sym_action_call"
android:id="@+id/dialBtn"/>
<ImageButton
android:layout_weight="1.3"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:srcCompat="@android:drawable/ic_menu_mylocation"
android:id="@+id/locationBtn"/>
<ImageButton
android:layout_weight="1.3"
android:layout_width="match_parent"
android:layout_height="wrap_content" card_view:srcCompat="@android:drawable/ic_menu_send"
android:id="@+id/whasappBtn"/>
<Button
android:text="Set Alarm"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/alarmBtn"/>
</LinearLayout>
关于android - 如何修复按钮对齐以在不同屏幕上具有相同的外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57365934/