我尝试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>
感谢您的输入另一个线性布局。
嗨,它的工作原理,但像我可能会误导你,东阳它的一个相当大的图像中的ImageButton的,所以如果我想让它从屏幕高度的2/5开始,因此它覆盖了屏幕的中心点。如果我使用你的建议答案,它会将按钮从中心推开,并且不允许它覆盖它。 – urSus 2012-07-27 15:10:40
嗯,也许尝试用paddingBottom替换paddingTop? – 2012-07-27 15:11:53
哦,我实际上读错了,在居中的假视图上设置填充不起作用。设置上面的按钮上的填充只是向上推按钮,这不是我的解决方案,因为我想覆盖中点 – urSus 2012-07-27 15:33:20