[英]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.