有没有一种方法可以使视图偏离中心? [英] Is there a way to offset a view from center?

查看:72
本文介绍了有没有一种方法可以使视图偏离中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将按钮的位置不完全居中,但是假设在屏幕高度的2/5s中,我没有成功找到属性,所以我尝试了这种方法

I'm trying to possition my button not exactly in center, but let's say in the 2/5s of the screen height, I was looking for attribute unsuccessfully, so I tried this approach

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

但是,即使我在假视图上设置了边距,它也不起作用.

However it does not work, even if I set margin on the fake view.

填充属性有效,但是它是一个大图像,如果我希望它从屏幕高度的2/5开始,它将覆盖屏幕的中心点,因此,如果我使用填充属性,它将起作用但将其推开从中心开始,不允许它遮盖.

Padding attribute works, however as it is a big image and if I want it to start at 2/5ths of screen height, it covers the center point of screen, so if I use padding attribute it works but it pushes it away from center and does not allow it to cover it.

尽管如此,我还是使用了LinearLayout使其工作,我想避免这种情况,因为在顶部和底部彼此之间有更多的视图,因此使用线性布局会导致嵌套视图.不幸的是,我认为这是唯一的选择.

Alhough, I made it work using LinearLayout, which I wanted to avoid because there are more Views on top and bottom next to each other so it would lead to nested views using linear layout. Unfortunately I think its the only option.

基本上,它使用另一种线性布局,用height=0dpweight=1填充顶视图和底视图未使用的剩余空间,并将其重力设置为中心.

It basically it uses another linear layout that fills the remaining space left unused by top and bottom views with height=0dp and weight=1 and sets its gravity to center.

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

推荐答案

根据目标设备的屏幕尺寸,X dp的填充可能会将其移动的高度超过其高度的五分之一.

Depending on the screen size of the target device, padding by X dp might move it more than just a fifth of the height.

您可能必须自己编写此动作的代码.但是,没有什么可以阻止您执行此操作:

You might have to code this movement yourself. However, nothing prevents you from doing:

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


</RelativeLayout>

这篇关于有没有一种方法可以使视图偏离中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆