有没有办法来抵消中心在Android的一个看法? [英] Is there a way to offset a view from center in Android?

查看:140
本文介绍了有没有办法来抵消中心在Android的一个看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试possition我的按钮不完全的中心,但可以说在屏幕高度的2/5秒,我一直在寻找的属性失败,所以我尝试这种方法

im trying to possition my button not exactly in center, but lets 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 / 5ths,它涵盖了屏幕的中心点,所以如果我使用的填充属性它工程,但它推动它远离中​​心而不允许它掩盖它。对不起我的坏

thanks for you answers guys, 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. Sorry my bad

不过,我做它的工作使用线性布局,这是我想避免,因为有在顶部和底部彼此相邻,因此会导致使用线性布局嵌套的看法更多观点。不幸的是我认为它是唯一的选择。

Though, i made it work using linear layout, which i wanted to avoid because there are more view 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.

这基本上它使用了与高度顶部和底部的观点填充闲置的剩余空间另一个线性布局= 0dp和重量= 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>

感谢您的输入。

Thanks for your input.

推荐答案

根据目标设备的屏幕尺寸,填充用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.

您可能需要code这个运动吧。

you might have to code this movement yourself.

但话又说回来,没有prevents你做

but then again, 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" />

这篇关于有没有办法来抵消中心在Android的一个看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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