2012-07-27 79 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

相關問題