Android - RelativeLayout中的ImageButton与父级大小不匹配

时间:2017-10-01 15:42:45

标签: android android-layout relativelayout

对于Android开发者来说相当新,我在使用RelativeLayout时遇到了麻烦。在下面的XML示例中,我希望我的ImageButton(在右侧,使用' +')匹配RelativeLayout高度。但是,它没有,如此图所示: Example 为什么没有' +'匹配最大可能的高度?

以下是相关代码:

      v        L2
30  0.2  0.152577
31  0.2  0.152577
32  0.2  0.152577
33  0.2  0.152577
34  0.2  0.341053
35  0.2  0.341053
36  0.2  0.341053
37  0.2  0.341053
38  0.2  0.341053
40  0.2  0.341053
41  0.2  0.341053
42  0.2  0.341053
43  0.2  0.341053
44  0.2  0.341053
45  0.2  0.341053
46  0.2  0.341053
48  0.2  0.341053
49  0.2  0.341053

感谢您的任何帮助或想法。

编辑:我想要的是占据线条高度的右键,即高度与中间的两个文字区域加上顶部和底部边距相同,不多也不少。

4 个答案:

答案 0 :(得分:3)

我从XML中可以理解,似乎你想要制作" +"按钮更可点击。 我建议你有一些填充,这应该做的工作。

编辑1:通过将ImageView的顶部和底部与相关的TextView对齐,您可以在不填充的情况下执行以下操作

<?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="wrap_content"
android:background="@drawable/artist_cell_background">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:src="@drawable/ic_music_note_24dp">
</ImageView>

<TextView
    android:id="@+id/artist_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:lines="1"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/black"
    android:layout_alignParentTop="true"
    android:paddingTop="10dp"
    android:layout_toRightOf="@id/imageView"
    android:layout_toEndOf="@id/imageView"
    android:layout_toLeftOf="@id/artist_button"
    android:layout_toStartOf="@id/artist_button">
</TextView>

<TextView
    android:id="@+id/number_of_albums"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:lines="1"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:paddingBottom="10dp"
    android:layout_toRightOf="@id/imageView"
    android:layout_toEndOf="@id/imageView"
    android:layout_toLeftOf="@id/artist_button"
    android:layout_toStartOf="@id/artist_button"
    android:layout_below="@id/artist_name">
</TextView>

<ImageButton
    android:id="@+id/artist_button"
    android:src="@drawable/ic_add_24dp"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/artist_name"
    android:layout_alignBottom="@id/number_of_albums"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@drawable/artist_cell_playlist_button_background">
</ImageButton>
</RelativeLayout>

答案 1 :(得分:1)

将您的相对布局放在应该这样做的线性布局中

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

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:src="@drawable/ic_music_note_24dp">
</ImageView>

<ImageButton
    android:id="@+id/artist_button"
    android:src="@drawable/ic_add_24dp"
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@drawable/artist_cell_playlist_button_background">
</ImageButton>

<TextView
    android:id="@+id/artist_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:lines="1"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/black"
    android:layout_alignParentTop="true"
    android:layout_marginTop="10dp"
    android:layout_toRightOf="@id/imageView"
    android:layout_toEndOf="@id/imageView"
    android:layout_toLeftOf="@id/artist_button"
    android:layout_toStartOf="@id/artist_button">
</TextView>

<TextView
    android:id="@+id/number_of_albums"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:lines="1"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_marginBottom="10dp"
    android:layout_toRightOf="@id/imageView"
    android:layout_toEndOf="@id/imageView"
    android:layout_toLeftOf="@id/artist_button"
    android:layout_toStartOf="@id/artist_button"
    android:layout_below="@id/artist_name">
</TextView>

</RelativeLayout>
</LinearLayout>

答案 2 :(得分:1)

您的图片可能很小。在android:scaleType="fitCenter"

中使用ImageButton

还尝试通过将它们设置为0dp来从ImageButton中删除默认填充和边距。

答案 3 :(得分:0)

在@rex的帮助下,我想出了如何做到这一点:将左图片和2个TextViews包含在RelativeLayout中(这将设置主RelativeLayout的大小),并将ImageButton的顶部和底部对齐到此布局。 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/artist_cell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/cell_background">

<ImageButton
    android:id="@+id/artist_plus_button"
    android:src="@drawable/ic_add_24dp"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:layout_alignTop="@+id/artist_infos"
    android:layout_alignBottom="@+id/artist_infos"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@drawable/plus_button_background"
    android:contentDescription="@string/artistPlusButtonCD">
</ImageButton>

<RelativeLayout
    android:id="@id/artist_infos"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/artist_plus_button"
    android:layout_toStartOf="@id/artist_plus_button">

    <ImageView
        android:id="@+id/album_cover"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_music_note_24dp"
        android:contentDescription="@string/albumCoverContentDescription">
    </ImageView>

    <TextView
        android:id="@+id/artist_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/album_cover"
        android:layout_toEndOf="@id/album_cover">
    </TextView>

    <TextView
        android:id="@+id/number_of_albums"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_marginBottom="10dp"
        android:layout_toRightOf="@id/album_cover"
        android:layout_toEndOf="@id/album_cover"
        android:layout_below="@id/artist_name">
    </TextView>
</RelativeLayout>

</RelativeLayout>