【问题标题】:How to align my logo in center irrespective or left and right reference in relative layout?如何在相对布局中将我的徽标对齐在中心而不考虑左右参考?
【发布时间】:2014-01-03 19:00:19
【问题描述】:

如何在不提供相对布局中的左右参考的情况下将我的标志对齐在中心?这可能吗?

Logo id 是“windowtitle”,当前对齐是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#e7ebee"
    android:gravity="center"
    android:orientation="vertical"
    android:weightSum="1" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="452dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0a2436"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:src="@drawable/back" />

            <ImageView
                android:id="@+id/inboxheader"
                android:layout_width="2dip"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
                android:layout_toRightOf="@+id/back"
                android:background="#85929B"
                android:paddingBottom="15dip"
                android:paddingTop="15dip" />

            <ImageView
                android:id="@+id/windowtitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_toRightOf="@+id/inboxheader"
                android:background="#0a2436"
                android:gravity="center_horizontal|center_vertical"
                android:paddingBottom="10dip"
                android:paddingTop="10dip"
                android:src="@drawable/logo" >
            </ImageView>
        </RelativeLayout>

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:divider="@android:color/darker_gray"
            android:dividerHeight="1dip"
            android:drawSelectorOnTop="false"
            android:focusable="false"
            android:paddingTop="0dip" />

        <!-- android:divider="@color/listDivider" -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerInParent="true"
            android:gravity="bottom" >

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#092435"
                android:gravity="center_horizontal" >

                <Button
                    android:id="@+id/btnManualLookup"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:background="#4982AE"
                    android:gravity="center"
                    android:padding="15dip"
                    android:text="Activity"
                    android:textColor="#ffffff" />

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="6dip"
                    android:layout_marginTop="6dip"
                    android:background="#85929B" />

                <Button
                    android:id="@+id/supportbutton"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:background="#092435"
                    android:gravity="center"
                    android:padding="15dip"
                    android:text="Support"
                    android:textColor="#ffffff" />

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="6dip"
                    android:layout_marginTop="6dip"
                    android:background="#85929B" />

                <Button
                    android:id="@+id/messagebutton"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:background="#092435"
                    android:gravity="center"
                    android:padding="15dip"
                    android:text="Messages"
                    android:textColor="#ffffff" />
            </TableRow>
        </TableLayout>
    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android layout alignment


    【解决方案1】:

    我已经解决了你的问题。我是 StackOverflow 的新用户。这很简单,我已经复制了您的代码并对您所说的问题进行了评论。否则你的代码是一样的,别担心我也用了你用过的同样的 id。所以,它也不会妨碍你的代码。

    现在完整布局的求解代码是:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#e7ebee"
        android:gravity="center"
        android:orientation="vertical"
        android:weightSum="1" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="452dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:background="#0a2436"
                >
              <!--<ImageView
                    android:id="@+id/back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/back" />
                <ImageView
                    android:id="@+id/inboxheader"
                    android:layout_width="2dip"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="5dp"
                    android:layout_toRightOf="@+id/back"
                    android:background="#85929B"
                    android:paddingBottom="15dip"
                    android:paddingTop="15dip" />
                <ImageView
                    android:id="@+id/windowtitle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_toRightOf="@+id/inboxheader"
                    android:background="#0a2436"
                    android:gravity="center_horizontal|center_vertical"
                    android:paddingBottom="10dip"
                    android:paddingTop="10dip"
                    android:src="@drawable/logo" >
                </ImageView>-->
                <ImageView
                    android:id="@+id/windowtitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/logo" 
                    android:contentDescription="@null"/>
                <ImageView
                    android:id="@+id/back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/back" />            
                <ImageView
                    android:id="@+id/inboxheader"
                    android:layout_width="2dip"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="5dp"
                    android:layout_toRightOf="@+id/back"
                    android:background="#85929B"
                    android:paddingBottom="15dip"
                    android:paddingTop="15dip" /> 
            </RelativeLayout>
            <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:divider="@android:color/darker_gray"
                android:dividerHeight="1dip"
                android:drawSelectorOnTop="false"
                android:focusable="false"
                android:paddingTop="0dip" />
            <!-- android:divider="@color/listDivider" -->
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" >
            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:gravity="bottom" >
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="#092435"
                    android:gravity="center_horizontal" >
                    <Button
                        android:id="@+id/btnManualLookup"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:background="#4982AE"
                        android:gravity="center"
                        android:padding="15dip"
                        android:text="Activity"
                        android:textColor="#ffffff" />
                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="6dip"
                        android:layout_marginTop="6dip"
                        android:background="#85929B" />
                    <Button
                        android:id="@+id/supportbutton"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:background="#092435"
                        android:gravity="center"
                        android:padding="15dip"
                        android:text="Support"
                        android:textColor="#ffffff" />
                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="6dip"
                        android:layout_marginTop="6dip"
                        android:background="#85929B" />
                    <Button
                        android:id="@+id/messagebutton"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:background="#092435"
                        android:gravity="center"
                        android:padding="15dip"
                        android:text="Messages"
                        android:textColor="#ffffff" />
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </LinearLayout>
    

    您将此 XML 布局复制并粘贴到您的项目中并享受 .如果我的回答对你有帮助,请支持这个回答。

    享受代码。

    【讨论】:

    • 非常感谢Satyaki。我非常感谢您的奉献精神和热情...继续前进...
    • 很高兴我的小改动对您有所帮助。我一直喜欢做编程。感谢您接受我的回答。
    • @SatyakiMukherjee 你也可以使用属性 android:layout_centerInParent="true"
    • @logosar Yaa,它会的。
    【解决方案2】:

    我可能会将RelativeLayout 设为LinearLayout 并给每个Views 一个weight。您可能需要稍微使用weight 才能获得您想要的东西,但它应该可以工作。类似的东西

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0a2436"
            android:gravity="center"  // I don't think this is needed 
            android:orientation="horizontal" >  
    
            <ImageView
                android:id="@+id/back"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/back" />
    
            <ImageView
                android:id="@+id/inboxheader"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:layout_centerVertical="true"
                android:background="#85929B"
                android:paddingBottom="15dip"
                android:paddingTop="15dip" />
    
            <ImageView
                android:id="@+id/windowtitle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:background="#0a2436"
                android:gravity="center_horizontal|center_vertical"
                android:paddingBottom="10dip"
                android:paddingTop="10dip"
                android:src="@drawable/logo" >
            </ImageView>
        </LinearLayout>
    

    我尚未对此进行测试,但您可以尝试一下,看看它是否能满足您的需求。正如我所说,您可能需要稍微使用一下layout_weight,但这可能会让您接近您想要的。

    【讨论】:

    • 感谢 codeMagic 付出的努力和时间,很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 2022-11-10
    • 2014-10-24
    相关资源
    最近更新 更多