Android Studio 设计视图和设备上按钮之间的距离不匹配的原因是什么?

huangapple go评论86阅读模式
英文:

Any reason why the distance between buttons do not match in design view and on device at Android Studio?

问题

以下是您要的翻译部分:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/createAccountBtn"
    android:layout_width="238dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="86dp"
    android:layout_marginTop="35dp"
    android:layout_marginEnd="87dp"
    android:layout_marginBottom="318dp"
    android:text="@string/create_account"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/signInBtn" />

<Button
    android:id="@+id/signInBtn"
    android:layout_width="237dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="89dp"
    android:layout_marginTop="282dp"
    android:layout_marginEnd="85dp"
    android:layout_marginBottom="14dp"
    android:text="@string/common_signin_button_text"
    app:layout_constraintBottom_toTopOf="@+id/createAccountBtn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

希望这可以帮助您。如果您需要进一步的翻译或帮助,请随时告诉我。

英文:

Android Studio 设计视图和设备上按钮之间的距离不匹配的原因是什么?

Here is the code I am using at the layout file. I am using Material components as theme style. Here I used both material component button and button, but I think they are both converted to material button due to my theming style:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/createAccountBtn"
    android:layout_width="238dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="86dp"
    android:layout_marginTop="35dp"
    android:layout_marginEnd="87dp"
    android:layout_marginBottom="318dp"
    android:text="@string/create_account"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/signInBtn" />

<Button
    android:id="@+id/signInBtn"
    android:layout_width="237dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="89dp"
    android:layout_marginTop="282dp"
    android:layout_marginEnd="85dp"
    android:layout_marginBottom="14dp"
    android:text="@string/common_signin_button_text"
    app:layout_constraintBottom_toTopOf="@+id/createAccountBtn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

答案1

得分: 0

模拟器模型和您设计应用程序的模型可能不同!
请检查并回复。

英文:

The emmulator model and the model for which you designed the app might be different !
Please check that and revert back .

答案2

得分: 0

显然,我需要在layout_widthlayout_height属性中使用match_parentwrap_content属性。可以查看此文档以了解如何在ConstraintLayout中使用它:https://developer.android.com/training/multiscreen/screensizes

但我建议在我的情况下使用LinearLayout,因为ConstraintLayout的行为仍然不佳,因为我不得不定义精确的dps边距属性,这可能与某些屏幕尺寸不对齐。我将在下面添加LinearLayout解决方案,它在纵向和横向模式下的表现与我预期的一样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/createAccountBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/create_account" />

    <Button
        android:id="@+id/signInBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/common_signin_button_text"/>
</LinearLayout>
英文:

Apparently I need to use match_parent and wrap_content properties for layout_width and layout_height. One can see this documentation for its use with ConstraintLayout here: https://developer.android.com/training/multiscreen/screensizes

But I would suggest using LinearLayout as ConstraintLayout in my case behaved still badly because I had to define margin properties in exact dps which may not align with a certain screen size. I am adding the LinearLayout solution below which worked as I expected in portrait and landscape mode as well:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:gravity=&quot;center&quot;
    android:orientation=&quot;vertical&quot;
    android:padding=&quot;16dp&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;Button
        android:id=&quot;@+id/createAccountBtn&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;@string/create_account&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/signInBtn&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;@string/common_signin_button_text&quot;/&gt;
&lt;/LinearLayout&gt;

huangapple
  • 本文由 发表于 2020年8月4日 05:03:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63236921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定