Android 如何使用RelativeLayout使视图居中?
我想知道如何使用RelativeLayout将视图居中放置在其他两个视图之间(或视图与父边之间) 例如,如果我有以下内容 如何使用RelativeLayout将按钮垂直居中放置在ImageView和屏幕底部之间 我正在寻找一个解决方案,其中Android 如何使用RelativeLayout使视图居中?,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我想知道如何使用RelativeLayout将视图居中放置在其他两个视图之间(或视图与父边之间) 例如,如果我有以下内容 如何使用RelativeLayout将按钮垂直居中放置在ImageView和屏幕底部之间 我正在寻找一个解决方案,其中 按钮没有以任何方式拉伸 没有嵌套的布局 我正试图在XML布局中实现这一点(不是以编程方式)。您可以使用以下内容: <RelativeLayout android:layout_below="@id/theImageView"
- 按钮没有以任何方式拉伸
- 没有嵌套的布局
<RelativeLayout
android:layout_below="@id/theImageView"
android:align_parentBottom="true"
android:layout_width="match_parent"
android:layout_height="200dp" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="onClickButton"
android:textSize="20sp"
android:text="Go"/>
</RelativeLayout>
在XML文件中使用android:layout\u centerInParent=“true”
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.8"
android:background="#00ff00" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.2" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
@Gus您可以在其他两个视图之间创建中心视图。。。这不完全是你必须尝试的 这边
<?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="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#00ccFF"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="59dp"
android:layout_height="59dp"
android:layout_alignRight="@+id/linearLayout1"
android:layout_alignTop="@+id/linearLayout1"
android:layout_marginRight="-21dp"
android:layout_marginTop="-21dp"
android:background="#FFccFF"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
使用下面的XMl代码,它将解决您的问题
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/mLlayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/mImgView1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="2" >
<Button
android:id="@+id/Btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Dipak" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
我认为您只需要像往常一样对齐imageview,并使用下面的布局对齐按钮,无需硬编码也无需使用嵌套视图在完成的按钮上使用android:gravity=“center”
<RelativeLayout
android:layout_below="@id/theImageView"
android:align_parentBottom="true"
android:layout_width="match_parent"
android:layout_height="200dp" >
<ImageView
android:id="@+id/imgview"
android:layout_width="match_parentmat"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imgview"
android:gravity="center"
android:onClick="onClickButton"
android:textSize="20sp"
android:text="Go"/>
</RelativeLayout>
不幸的是,没有简单的方法可以做到这一点。您可以嵌套布局,也可以在运行时嵌套布局。最简单的方法是嵌套布局:
<LinearLayout
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_top_image"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/my_button_label"/>
</RelativeLayout>
</LinearLayout>
这会将图像置于顶部。在此下方,
RelativeLayout
上的layout\u height=0
和layout\u weight=1
属性会占用所有剩余空间。然后,您可以将按钮置于相对位置yout
的中心。您可以使用按钮上的填充物使其达到您想要的大小。这不就是将按钮放在TextView的正下方吗?在上面的布局中,似乎没有考虑屏幕的底部。我需要在文本视图和屏幕底部之间垂直居中的按钮。嗨,Shrikant,对不起,我看不到该布局的作用。它不再提及ImageView。如果您定义的RelativeLayout是根布局,那么这将使整个窗口中的按钮居中。这实际上是使用线性布局来完成工作,而不是RelativeLayout。
<LinearLayout
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_top_image"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/my_button_label"/>
</RelativeLayout>
</LinearLayout>