代码之家  ›  专栏  ›  技术社区  ›  Jacques Krause

将工具栏移动到相对布局的顶部

  •  0
  • Jacques Krause  · 技术社区  · 10 年前

    我试着把按钮放在中间,但把工具栏放在顶部。目前,由于重力,两者都在中间 "center" .

    有没有一种不使用填充属性的方法?否则,它在其他屏幕尺寸上看起来会有所不同。

    这是我的 main_activity :

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_gravity="center">
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="#2196F3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
    </android.support.v7.widget.Toolbar>
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    
    
    </RelativeLayout>
    
    4 回复  |  直到 9 年前
        1
  •  1
  •   Anderson K    10 年前

    很简单,改为:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >
    
        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="#2196F3"
            android:minHeight="?attr/actionBarSize" >
        </android.support.v7.widget.Toolbar>
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:text="New Button" />
    
    </RelativeLayout>
    
        2
  •  1
  •   kevz    10 年前

    在xml下面尝试

    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="#2196F3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    
    </android.support.v7.widget.Toolbar>
    
    <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="New Button" />
    
    </RelativeLayout>
    
        3
  •  0
  •   Alexander    10 年前

    尝试 android:layout_centerInParent="true" 为您的按钮

    用于工具栏尝试 android:layout_alignParentTop="true"

        4
  •  0
  •   scubasteve623    10 年前

    为工具栏创建单独的布局,并将其重力设置为顶部。