java - 使布局适应所有屏幕分辨率 - Android

标签 java android xml android-layout

我正在尝试创建一个在单击按钮时显示的表单。这是输出: enter image description here

如您所见,主要问题有两个:

  1. 绿色按钮位于右侧而不是表单的中心;

  2. 表单底部的白色部分,根据设备的分辨率而变大。我不知道为什么会出现。

这是表单的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/MyDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">

<Button
    android:id="@+id/button_form"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/dateSelect"
    android:layout_centerInParent="false"
    android:layout_marginLeft="@dimen/_16sdp"
    android:layout_marginTop="@dimen/_5sdp"
    android:layout_marginBottom="@dimen/_7sdp"
    android:background="@drawable/button_bg"
    android:text="OK"
    android:textAlignment="center"
    android:textColor="@color/whiteTextColor"
    android:textSize="@dimen/_7sdp">

</Button>

<Button
    android:id="@+id/dateSelect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/genderRadioGroup"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="Inserisci l'età"
    android:textColor="#050505"
    android:theme="@style/Button2Style"/>

<RadioGroup
    android:layout_marginTop="@dimen/_8sdp"
    android:id="@+id/genderRadioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


    <RadioButton
        android:id="@+id/maleRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="@dimen/_3sdp"
        android:text="Maschio" />


    <RadioButton
        android:id="@+id/femaleRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/maleRadioButton"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="@dimen/_1sdp"
        android:text="Femmina" />
</RadioGroup>

最佳答案

请检查下面的代码,我已删除样式,请在下面的代码中添加它

<androidx.constraintlayout.widget.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="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <Button
        android:id="@+id/button_form"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/dateSelect"
        android:layout_centerInParent="false"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:text="OK"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/dateSelect">

    </Button>

    <Button
        android:id="@+id/dateSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/genderRadioGroup"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Inserisci l'età"
        android:textColor="#050505"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/genderRadioGroup" />

    <RadioGroup
        android:id="@+id/genderRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <RadioButton
            android:id="@+id/maleRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Maschio" />


        <RadioButton
            android:id="@+id/femaleRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/maleRadioButton"
            android:layout_alignParentLeft="true"
            android:text="Femmina" />
    </RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout> 

关于java - 使布局适应所有屏幕分辨率 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58854560/

相关文章:

java - Text to Speech 获取系统默认音调和语速

java - 这段 Java XML 解析代码有什么问题?

javascript - $.parseXML() 在 IE 8 以上的 IE 版本中不起作用

android - 图标集可见复制菜单

android - Activity 从模拟器中的主屏幕启动器恢复,但不是在 Android 设备上

android - java.lang.NullPointerException : CameraUpdateFactory is not initialized, 调用 "onResume"上的方法

java - Android XML 中的多个错误

Javafx Stackpane 显示类似 Java Swing 的 CardLayout

java - 如何确定调用 Activity 的 channel ?

java - Android Eclipse Classpath - 想要添加类路径容器路径但 Eclipse 不会让我