开发者

problem with 2 elements in android layout

I have the following layout for two things -> a back button and a title text view.the back button is supposed to to align to the left of the parent while the text is supposed to be at the centre. Somehow, its not working and I dont have any clue why. Can anyone kindly help me out ? Thanks.

            <?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"
                android:gravity="center_horizontal"
                android:orientation="horizontal">
                <ImageButton
                    android:src="@drawable/backtap"
                    android:background="@nu开发者_运维问答ll"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingLeft="5dip"
                    android:paddingRight="5dip"
                    android:id="@+id/back_button3"
                    android:layout_alignParentLeft="true"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingTop="10dip"
                    android:paddingLeft="10dip"
                    android:paddingRight="10dip"
                    android:layout_centerHorizontal="true"
                    android:id="@+id/title_text_view_success3"/>

            </RelativeLayout>


I changed it to a linear layout, changed layout_width+layout_height to wrap_content (instead of match_parent) and changed gravity to layout_gravity.

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

    <ImageButton
        android:src="@drawable/backtap"
        android:background="@null"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:id="@+id/back_button3"
        android:layout_alignParentLeft="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingTop="10dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:layout_centerHorizontal="true"
        android:text="text here"
        android:id="@+id/title_text_view_success3"/>

    </LinearLayout>

Seems to work

Hope it helps.

EDIT

After re-reading your question, this might be of more help:

<?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="wrap_content">

    <ImageButton
        android:src="@drawable/backtap"
        android:background="@null"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:id="@+id/back_button3"
        android:layout_alignParentLeft="true"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:gravity="center_horizontal"
        android:text="text here"
        android:id="@+id/title_text_view_success3"/>

</LinearLayout>


Try using a LinearLayout. Also try removing android:gravity from the parent layout.


Thanks for your help/efforts. I took into considerations all your tricks and tips and made some changes to my layout and it works nicely now. Thanks once again.:)

            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center_horizontal">
                <ImageButton
                    android:src="@drawable/backtap"
                    android:background="@null"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingLeft="5dip"
                    android:paddingRight="5dip"
                    android:id="@+id/back_button3"
                    android:layout_alignParentLeft="true"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingTop="10dip"
                    android:paddingLeft="10dip"
                    android:paddingRight="10dip"
                    android:layout_centerHorizontal="true"
                    android:id="@+id/title_text_view_success3"/>

            </RelativeLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜