相关疑难解决方法(0)

有没有办法在Android中偏移中心的视图?

我试图使我的按钮不完全在中心,但让我们说在屏幕高度的2/5s,我正在寻找属性失败,所以我试过这种方法

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:padding="20dp" >

 <ImageButton
    android:id="@+id/flashlight_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/fakeView"
    android:background="@null"
    android:contentDescription="@string/flashlight_button_description"
    android:src="@drawable/freeml_bright" />

   <View
    android:id="@+id/fakeView"
    android:layout_width="10dp"
    android:layout_height="10dp"
    android:layout_centerInParent="true"
    android:background="#FFAABB" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

但是它不起作用,即使我在假视图上设置了保证金.

有任何想法吗?

//编辑//

谢谢你的回答家伙,填充属性工作,但因为它是一个大图像,如果我希望它从屏幕高度的2/5开始,它覆盖屏幕的中心点,所以如果我使用填充属性它的工作,但它将它推离中心,不允许它覆盖它.对不起这是我的错

虽然,我使用线性布局工作,我想避免,因为在顶部和底部有更多的视图彼此相邻,所以它将导致使用线性布局的嵌套视图.不幸的是,我认为这是唯一的选择.

它基本上使用另一个线性布局,填充顶部和底部视图未使用的剩余空间,高度= 0dp,权重= 1,并将其重力设置为中心

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/application_logo_description"
        android:src="@drawable/mylight" />

     <ImageButton
        android:id="@+id/settings_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@null"
        android:contentDescription="@string/settings_button_description"
        android:src="@drawable/settings_button" /> 
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/flashlight_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="@string/flashlight_button_description" …
Run Code Online (Sandbox Code Playgroud)

android center android-layout android-view android-relativelayout

24
推荐指数
2
解决办法
2万
查看次数