android - 如何修复按钮对齐以在不同屏幕上具有相同的外观

标签 android android-layout

我有下面的布局,但是我在一行中制作的按钮在屏幕之间显示不正确:

<?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 不能正确显示,具体取决于屏幕尺寸/分辨率:

正确的布局:

correct layout

错误的布局:

wrong layout

最佳答案

对 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/

相关文章:

android - 如何在此 android 示例中添加 TitleBar?

android - IsolatedContext 与 AndroidTestCase.getContext()

java - 我无法在 Android 的 Baseadapter 中使用 viewgroup

android - 在 View 中动态添加 fragment

android - 向左浮动 TextView 和 TableLayout

android - 相对布局字段的放置不当

android - 如何禁用 DatePickerDialog 的标题?

java - 如何检测 html 字符串中的第一个和倒数第二个 p 标签

java - HttpURLConnection 打开跟踪文件时出错 : no such file or directory

java - 获取 java.lang.IllegalStateException : Could not execute method of the activity after adding BigDecimal varibles.