按钮在水平线性布局中的微调器之后,则无法正确显示



我有一个xamarin中android的axml布局,代码为:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="20px"
android:id="@+id/space1" />
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/text"
android:paddingLeft="25px"
android:paddingTop="10px"
android:paddingRight="15px" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/button1" />
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15px"
android:id="@+id/spinner" />

</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="25px"
android:id="@+id/space2" />
<EditText
android:gravity="top"
android:hint="long text"    
android:inputType="textLongMessage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/editText1" />

它正在发挥作用。

前的布局

但我需要按钮在右上角,所以我试着把它放在同一水平线性布局的微调器后面(这里只是嵌套线性布局的代码(:

<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/text"
android:paddingLeft="25px"
android:paddingTop="10px"
android:paddingRight="15px" />
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15px"
android:id="@+id/spinner" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/button1" />
</LinearLayout>

在那之后,布局看起来很奇怪:

后的布局

这里怎么了?为什么按钮会出现这种情况?我该怎么解决这个问题?

编辑:上面的布局代码只是一个片段。下面是我使用这个片段的活动代码:

<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimaryDark">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

这种布局是由微调器中的高度匹配父项引起的,因为它占用了所有空间,并且不让按钮占据任何位置。如果你用150dp这样的固定大小或包装内容来调整它,它就会起作用。

最新更新