似乎很简单。我想button2匹配button1。但是当我插入" match_parent"时,button2与主布局匹配,这导致"满刻度"到了两边。我该如何解决?
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Catálogo de Drogas"
android:textStyle="bold"
android:id="@+id/button"
android:layout_gravity="center"
android:layout_marginTop="60dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mixador"
android:textStyle="bold"
android:id="@+id/button2"
android:layout_gravity="center"
android:layout_marginTop="120dp"/>
答案 0 :(得分:0)
button
不是button2
的父级,他们在公共父级布局中处于同一级别。
如果我理解正确(您的问题不清楚),您希望button2
宽度与button
宽度匹配(即wrap_content)。
如果是,并且父布局是RelativeLayout
,您可以将button2
与button
对齐
<RelativeLayout (...)>
<Button
android:id="@+id/button"
android:text="Catálogo de Drogas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
(...)
/>
<Button
android:id="@+id/button2"
android:text="Mixador"
android:layout_width="match_parent"
android:layout_height="wrap_content"
(...)
android:layout_alignLeft="@+id/button"
android:layout_alignRight="@+id/button"
/>
</RelativeLayout>
如果父按钮是LinearLayout
,那么诀窍就是使wrap_content
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button"
android:text="Catálogo de Drogas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
(...)
/>
<Button
android:id="@+id/button2"
android:text="Mixador"
android:layout_width="match_parent"
android:layout_height="wrap_content"
(...)
/>
</LinearLayout>