2011-10-29 13 views
11

relative layoutसापेक्ष लेआउट। 2 आइटम

मैं 2 अन्य वस्तुओं के बीच एक आइटम कैसे रख सकता हूं और इसे केंद्र में संरेखित कर सकता हूं? (कृपया ऊपर दी गई तस्वीर में लाल बटन देखें) - मैं इसे "सेंटर बटन" और "नीचे बटन" के बीच कैसे रख सकता हूं?

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"> 

    <Button 
     android:id="@+id/button_center" 
     android:text="Center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_centerInParent="true"/> 

    <!-- The new button should be between these 2 items --> 

    <Button 
     android:id="@+id/button_bottom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Bottom" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true"/> 

    <Button 
     android:id="@+id/button_top" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Top" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"/> 

    <Button 
     android:id="@+id/button_left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Left" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true"/> 

    <Button 
     android:id="@+id/button_rignt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Right" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true"/> 

    <Button 
     android:id="@+id/button_rel_right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/button_right" 
     android:layout_alignTop="@id/button_rignt" 
     android:text="RelRight"/> 

    <Button 
     android:id="@+id/button_rel_left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/button_left" 
     android:layout_alignTop="@id/button_left" 
     android:text="RelLeft"/> 

</RelativeLayout> 
+1

अपने लेआउट के xml पोस्ट करें। समाधान आपके एक्सएमएल पर निर्भर करता है –

उत्तर

24

आपको एक और लेआउट जोड़ने की आवश्यकता होगी ताकि बटन बिल्कुल बीच में हो। उदाहरण के लिए, इसे अपने लेआउट में जोड़ें:

<FrameLayout 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_above="@+id/button_bottom" 
    android:layout_alignLeft="@+id/button_center" 
    android:layout_alignRight="@+id/button_center" 
    android:layout_below="@id/button_center" > 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="new" /> 
</FrameLayout> 
-1

मैं इस समाधान के लिए आया था:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <!-- centered button --> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <Button android:text="CenteredButton" 
       android:id="@+id/centered_button"   
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center"/> 

    </FrameLayout> 
    </LinearLayout> 

</RelativeLayout> 

बस बटन के बजाय LinearLayout साथ इस कोड का उपयोग

यहाँ मेरी रिश्तेदार लेआउट कोड है। मुझे लगता है कि यह सबसे अच्छा नहीं है, लेकिन यह काफी अच्छा है।

संबंधित मुद्दे