RelativeLayout中的按钮大小和填充

时间:2014-01-27 23:29:37

标签: android android-layout

我正在尝试创建类似于Google音乐应用的标题+按钮,例如在左边有一个“歌曲”标题,然后在右边有一个按钮,文字“X more”..

我使用RelativeLayout作为TextView和Button

我的问题是该按钮占用了包含高度错误的文本的布局大小,并且填充似乎没有做任何事情。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        [REMOVED for clarity]

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/list_foreground"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="@string/photos"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <Button
                android:id="@+id/photo_button"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:background="@color/actionbar_background"
                android:padding="10dp"
                android:text="test" />
        </RelativeLayout>

我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

RelativeLayouts旨在让儿童参与布局&#34;亲戚&#34;对彼此。换句话说,如果你想让Button位于Textview的右边,你需要告诉它。

因为您正在相对于父LEFT / RIGHT进行对齐,所以事情似乎是&#34;有点&#34;工作

根据您的需要,使用LinearLayout可能会更好。 LinearLayouts使用&#34; orientation&#34;不是RelativeLayouts。

您应该查看一些教程(例如:http://mobile.tutsplus.com/tutorials/android/android-layout/),但最终您可能会先将按钮放在文本视图中,然后将文本视图内容适当地换行。

答案 1 :(得分:0)

为了获得与音乐应用程序相同的效果我最终使用了RelativeLayout而不是按钮我使用的是另一个TextView,这给人的印象是它是一个按钮,但它为我提供了更多的格式化背景等的范围。我想只需在代码中设置一个OnClickListener

         <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:id="@+id/photo_title">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="10dp"
                android:text="@string/photos"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/more_photo_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:background="@color/actionbar_background"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:text="10 MORE"
                android:textColor="@color/button_text"
                android:textSize="12sp" />
        </RelativeLayout>