繁体   English   中英

Android:将相对布局居中对齐

[英]Android: align relative layout to center

疯狂地尝试将整个相对布局与屏幕中心对齐。

这是我需要对齐的dashboard_fragment:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <TextView
        android:id="@+id/counter_day"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="6dip" />

    <TextView
        android:id="@+id/next_event_placeholder"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_toRightOf="@id/counter_day"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:singleLine="true"
        android:ellipsize="marquee" />

    <TextView
        android:id="@+id/last_event_placeholder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/counter_day"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_above="@id/next_event_placeholder"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:text="My Application" />

</RelativeLayout>

这是一个片段,注入到 base_layout 中:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        >
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            />
        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>

    </LinearLayout>
    <!--
        IF YOU NEED HEADER:
        app:headerLayout="@layout/drawer_header"
    -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:menu="@menu/drawer"
        />
</android.support.v4.widget.DrawerLayout>

这是一个非常基本的布局,我会:

在此处输入图片说明

但目前我的布局在屏幕的左上角。

非常感谢。

在 RelativeLayout 上使用android:layout_gravity="center" ,或者在 FrameLayout 上使用android:gravity="center" (或两者都使用)。

修改成这样:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">

<TextView
    android:id="@+id/counter_day"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="6dip" />

<TextView
    android:id="@+id/next_event_placeholder"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_toRightOf="@id/counter_day"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:singleLine="true"
    android:ellipsize="marquee" />

<TextView
    android:id="@+id/last_event_placeholder"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/counter_day"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_above="@id/next_event_placeholder"
    android:layout_alignWithParentIfMissing="true"
    android:gravity="center_vertical"
    android:text="My Application" />

 </RelativeLayout>

只有你必须为相对布局放置重力。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM