【发布时间】:2019-12-22 16:13:26
【问题描述】:
我的布局设计在 Android Studio 预览版和我的手机(三星 s8)中不一样。 我正在尝试使两个按钮都水平对齐(即以相同的 x asix 开始和结束)。根据设计师的说法,它似乎按照我想要的方式对齐,但实际上(在设备中)它们不是。
Android Studio Designer 的屏幕截图:
三星 S8 截图:
我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Menu"
android:textSize="50sp" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:text="Select a layout type to view an example. The onClick attribute of each button will call a method which executes setContentView to load the new layout"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.59"
android:text="Load ConstraintLayout"
android:layout_marginLeft="8dp"
android:textSize="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.41"
android:text="Load"
android:layout_marginRight="8dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:text="Load TableLayout"
android:layout_marginLeft="8dp"/>
<Button
android:id="@+id/button2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Load"
android:layout_marginRight="8dp"/>
</LinearLayout>
<RatingBar
android:id="@+id/ratingBar4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="75dp"/>
</LinearLayout>
有人能理解不一致的原因吗?
【问题讨论】:
-
在两个线性布局中对文本视图(0.7)和按钮(0.3)使用相同的权重。您可以修改重量,但在两种布局中保持相同。我建议使用约束布局。
-
@Niraj,您的解决方案已成功。但只有在我将所有四个视图的 layout_width 属性设置为零之后。但是有没有合理的解释为什么设计师和移动端没有呈现相同的结果?
标签: android android-layout android-designer