Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android layout 设置两个线性布局,一个在按钮处对齐,另一个在屏幕其余部分居中_Android Layout_Xamarin_Xamarin.android - Fatal编程技术网

Android layout 设置两个线性布局,一个在按钮处对齐,另一个在屏幕其余部分居中

Android layout 设置两个线性布局,一个在按钮处对齐,另一个在屏幕其余部分居中,android-layout,xamarin,xamarin.android,Android Layout,Xamarin,Xamarin.android,我对安卓系统的设计非常陌生,在建立一个 简单布局: 以下是我的目标: 我想在屏幕的末端有两个按钮,每个按钮大小相同,填充屏幕的宽度。 第二个线性布局应填充屏幕的其余部分,但其所有内容应垂直居中。 我尽了最大努力,但有时我的底部线性布局完全消失,有时底部的一个看起来不错,但其他布局内容没有居中。我只是不知道该尝试什么了。以下是我到目前为止得到的信息: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi

我对安卓系统的设计非常陌生,在建立一个 简单布局:

以下是我的目标:

我想在屏幕的末端有两个按钮,每个按钮大小相同,填充屏幕的宽度。 第二个线性布局应填充屏幕的其余部分,但其所有内容应垂直居中。 我尽了最大努力,但有时我的底部线性布局完全消失,有时底部的一个看起来不错,但其他布局内容没有居中。我只是不知道该尝试什么了。以下是我到目前为止得到的信息:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center">
    <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:id="@+id/editTableName"
      android:hint="@string/TableNameHint" />
    <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:id="@+id/editTableDescripion"
      android:hint="@string/TableDescriptionHint" />
    <EditText
      android:inputType="textPassword"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:hint="@string/TablePasswordHint"
      android:id="@+id/TablePassword"
      android:fontFamily="sans-serif" />
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom">
    <Button
      android:id="@+id/BtnAddTable"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/BtnAddTable" />
    <Button
      android:id="@+id/BtnCancelAddTable"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/BtnCancelAddTable"
      />
  </LinearLayout>
</LinearLayout>

好吧,您要做的是通过向组合中添加相对布局来实现:

你要做的是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="6"
 android:layout_gravity="center">
<EditText
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:id="@+id/editTableName"
  android:hint="@string/TableNameHint" />
<EditText
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:id="@+id/editTableDescripion"
  android:hint="@string/TableDescriptionHint" />
<EditText
  android:inputType="textPassword"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:hint="@string/TablePasswordHint"
  android:id="@+id/TablePassword"
  android:fontFamily="sans-serif" />
</LinearLayout>
<RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true">
<LinearLayout
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
>
<Button
  android:id="@+id/BtnAddTable"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:text="@string/BtnAddTable" />
<Button
  android:id="@+id/BtnCancelAddTable"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:text="@string/BtnCancelAddTable"
  />
 </LinearLayout>
 </RelativeLayout>
 </LinearLayout>
相对长度:

Android开发者官方网站说:

RelativeLayout是在相对位置显示子视图的视图组。每个视图的位置可以指定为相对于同级图元的位置,例如相对于另一个视图的左侧或下方,或者指定为相对于父相对区域的位置,例如相对于底部、左侧或中心对齐

我尽了最大努力,但有时我的底部线性布局完全消失,有时底部的一个看起来不错,但其他布局内容没有居中

您的按钮有时会消失,因为顶部子线布局的高度为match_parent(匹配父视图),这使其填充父视图的高度并覆盖下面的视图

您需要的是如下所示的布局结构:

说明:

因此,就像图片中的描述一样,您可以将weightsum设置为父布局,并将权重百分比设置为子布局。并使用Relativelayout包装按钮的线性布局,以便将按钮居中

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="4">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           android:layout_gravity="center"
            android:id="@+id/editTableName"
            android:hint="@string/TableNameHint" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/editTableDescripion"
            android:hint="@string/TableDescriptionHint" />
        <EditText
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:hint="@string/TablePasswordHint"
            android:id="@+id/TablePassword"
            android:fontFamily="sans-serif" />
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="bottom">
            <Button
                android:id="@+id/BtnAddTable"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/BtnAddTable" />
            <Button
                android:id="@+id/BtnCancelAddTable"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/BtnCancelAddTable" />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>
简短解释: 将android.support.constraint.Guideline与app:layou_constraintGuide_percent=0.6一起使用,画一条不可见的线来帮助定位按钮。指南将与顶部屏幕保持60%的距离。然后让按钮的堆栈布局成为指导方针的底部


有关ConstraintLayout的详细用法,请参阅。

该方法在理论上是有意义的。你试过了吗?我以1:1的比例复制了它,但它仍然没有显示我的两个按钮。这将起作用。我在这里看到的问题是,我的按钮实际上会占用比它们需要的更多的空间。他们将根据根布局的大小获得一定百分比的空间,而不是他们实际要求/需要的大小。@Jannik按钮的宽度现在是整个屏幕的50%,只有按钮的高度在包装内容。你的意思是你希望按钮的高度是根布局的某个百分比吗?这正是我不理解的。在您的示例中,relativelayout具有android:layout_weight=3,这不意味着它将占据整个显示器高度的3/4吗?这将是一个问题,因为它应该只占用实际需要的空间。我明白了,您可能想在底部区域插入一些其他视图。然后您可以使用ConstraintLayout作为解决方案,请检查我的更新。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <EditText
      android:id="@+id/editText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Name" />

  <EditText
      android:id="@+id/editText2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Name"
      app:layout_constraintTop_toBottomOf="@+id/editText" />

  <EditText
      android:id="@+id/editText3"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:layout_constraintTop_toBottomOf="@+id/editText2" />

  <android.support.constraint.Guideline
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/guideline"
      app:layout_constraintGuide_percent="0.6"
      android:orientation="horizontal"/>
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:layout_constraintBottom_toBottomOf="@id/guideline"
      android:orientation="horizontal">
  <Button
      android:id="@+id/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Button"/>
  <Button
      android:id="@+id/button2"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Button"/>
  </LinearLayout>
</android.support.constraint.ConstraintLayout>