RelativeLayout中的Android-ImageButton与父级大小不匹配
对于Android开发人员来说,我是一个新手,我在相对论方面遇到了麻烦。在下面的XML示例中,我希望右边的ImageButton和“+”匹配RelativeLayout高度。但是,它没有,如图所示: 为什么带“+”的右按钮与最大可能高度不匹配 以下是相关代码:RelativeLayout中的Android-ImageButton与父级大小不匹配,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,对于Android开发人员来说,我是一个新手,我在相对论方面遇到了麻烦。在下面的XML示例中,我希望右边的ImageButton和“+”匹配RelativeLayout高度。但是,它没有,如图所示: 为什么带“+”的右按钮与最大可能高度不匹配 以下是相关代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro
<?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>
<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>
谢谢你的帮助和建议
编辑:我想要的是右键占据行的高度,即与中间的两个文本区域加上上上下边距的高度相同,不超过或低于此高度。将您的相对布局放在一个线性布局中,这样可以做到这一点
<?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>
将您的相对布局放在一个线性布局中,这样可以做到这一点
<?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>
你的图像可能很小。在ImageButton中使用android:scaleType=fitCenter
还可以尝试通过将默认填充和边距设置为0dp来删除ImageButton中的默认填充和边距。您的图像可能很小。在ImageButton中使用android:scaleType=fitCenter
还可以尝试通过将ImageButton的默认填充和边距设置为0dp来删除它们。正如我从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>
正如我从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>
在@rex的帮助下,我想出了如何做到这一点:将左侧图片和两个文本视图括在一个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>
在@rex的帮助下,我想出了如何做到这一点:将左侧图片和两个文本视图括在一个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>
这对我不起作用,结果是一样的。为什么要改变结果?由于ImageButton的父级仍然是RelativeLayoutIt不适用于我,因此结果是相同的。为什么要改变结果?由于ImageButton的父级仍然是RelativeLayoutIt不适用于我,同样的结果。图像确实很小,但由于我没有要求它包装图像,图像的大小有什么关系?尝试将边距和填充设置为0dp。我想这不是边距和边距问题,因为当我硬编码图像按钮的高度(例如100dp)时,它占据了整行,没有边距。这对我不起作用,结果是一样的。图像确实很小,但我没有要求它包装图像,图像的大小有什么关系?尝试将边距和填充设置为0dp。我想这不是边距或边距问题,因为当我硬编码图像按钮的高度(例如100dp)时,它占据了整行,没有边距。你是对的,它可以工作,但这是一种变通办法。我想让“+”按钮占据整条线的高度,不管它有多大。如果用户更改文本大小,从而更改行大小,则此技巧将不起作用。如果您确实试图使“+”按钮占据整个高度。你可以让android:layout\u height=match\u Parents回答这个问题有点困难,因为你已经让RelativeLayout的高度取决于它的子级的高度,然后你指定它的一个子级具有父级的高度……这是不明确的,我们可能无法证明在不同的Android版本和手机上工作是合理的……有时这会使一行内容填满整个手机屏幕。您首先需要确定定义相对高度的视图。比如说,垂直方向的两个文本视图可能包含在一个线性布局中,然后你可以使可点击图片的顶部和底部与两个文本视图的顶部和底部对齐,这样它就可以直接获得父视图的高度。这不是一个更好的解决方案,可以包含线性布局…但更易于管理。我们应该尽量减少设计中的层次结构,但在您的场景中,这并不重要您是对的,它可以工作,但这是一种变通方法。我想让“+”按钮占据整条线的高度,不管它有多大。如果用户更改文本大小,从而更改行大小,则此技巧将不起作用。如果您确实试图使“+”按钮占据整个高度。你可以让android:layout\u height=match\u parents回答这个问题有点困难,因为你已经让RelativeLayout的高度取决于它的子级的高度,然后你指定它的一个子级具有父级的高度……这是ambi
guous,我们可能无法证明在不同的Android版本和手机上工作是合理的…有时这会使一行填满整个手机屏幕。您首先需要确定定义相对高度的视图。比如说,垂直方向的两个文本视图可能包含在一个线性布局中,然后你可以使可点击图片的顶部和底部与两个文本视图的顶部和底部对齐,这样它就可以直接获得父视图的高度。这不是一个更好的解决方案,可以包含线性布局…但更易于管理。我们应该尽量减少设计中的层次结构,但在您的场景中,这并不重要