2012-07-27 82 views
22

我尝试possition我的按钮在中心不完全是,但让在屏幕高度的2/5秒说,我一直在寻找的属性失败,所以我尝试这种方法有没有办法在Android中从中心偏移视图?

<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> 

但它不工作,即使我在假视图上设置了保证金。

任何想法?

// //编辑

感谢你的回答家伙,填充属性的作品,但因为它是一个大的图像,如果我希望它在屏幕高度的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" 
     android:src="@drawable/flashlight_button_selector" /> 

    <View 
     android:id="@+id/fakeView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginTop="60dp" 
     android:background="#FFAABB" /> 
</LinearLayout> 

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|center_horizontal" 
    android:contentDescription="@string/powered_by_description" 
    android:src="@drawable/powered_by" /> 

<ImageButton 
    android:id="@+id/ad_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|center_horizontal" 
    android:background="@null" 
    android:contentDescription="@string/ad_button_description" 
    android:src="@drawable/freeml" /> 

</LinearLayout> 

感谢您的输入另一个线性布局。

回答

3

不要使用余量,使用填充(上图),像这样:

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

这应该工作,虽然我没有测试它!

+0

嗨,它的工作原理,但像我可能会误导你,东阳它的一个相当大的图像中的ImageButton的,所以如果我想让它从屏幕高度的2/5开始,因此它覆盖了屏幕的中心点。如果我使用你的建议答案,它会将按钮从中心推开,并且不允许它覆盖它。 – urSus 2012-07-27 15:10:40

+0

嗯,也许尝试用paddingBottom替换paddingTop? – 2012-07-27 15:11:53

+0

哦,我实际上读错了,在居中的假视图上设置填充不起作用。设置上面的按钮上的填充只是向上推按钮,这不是我的解决方案,因为我想覆盖中点 – urSus 2012-07-27 15:33:20

3

尝试使用属性机器人:layout_toLeftOf =“@ + ID/fakeView”

+0

+1这太棒了! – 2013-03-08 18:19:45

32

根据目标设备的屏幕尺寸,填充由X DP可能移动它不仅仅是一个高度的第五更多。

你可能不得不自己编码这个动作。

但话又说回来,没有什么能阻止你做

<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" > 

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

<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:marginBottom="50dp" 
android:background="@null" 
android:contentDescription="@string/flashlight_button_description" 
android:src="@drawable/freeml_bright" /> 

+0

+1很棒。 – 2013-03-08 18:20:06

+0

谢谢你亲切的先生:) – Shark 2013-03-11 16:27:02

+5

在中间的假景!这是在箱子外面思考,正是我需要的! :) – 2014-04-25 16:49:33

相关问题