2014-01-27 81 views
0

我试图创建一个类似于Google Music App的Heading +按钮,例如哪里有“诗经”头在左边,然后在右侧有一个与文本“X多”的按钮..RelativeLayout中的按钮大小和填充

我使用的RelativeLayout为TextView的和按钮

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

<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> 

我在这里做错了什么?

回答

0

RelativeLayouts旨在让孩子在布局中“相对”。换句话说,如果你想让按钮在Textview的右边,你需要告诉它。

因为你正在相对于父LEFT/RIGHT对齐,所以看起来事情是“种”的工作。

根据您的需要,使用LinearLayout可能会更好。 LinearLayouts使用“orientation”而不是RelativeLayouts。

你应该看看一些教程(比如这个:http://mobile.tutsplus.com/tutorials/android/android-layout/),但最终你可能会先把你的按钮放在第一个,然后是文本视图,这样textview的内容会适当地包装。

0

为了获得与音乐应用程序相同的效果,我最终使用了RelativeLayout而不是Button我使用另一个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>