Android 如何根据父布局内容高度动态调整图像高度?
我有一个android:layout\u height=“wrap\u content”的父布局,在我的类代码中,它应该设置特定图像的布局背景,但图像高度大于内容高度。我想将背景的图像高度调整为与父布局内容高度相同的大小。我该怎么做 黑色-父布局 绿色-布局内容 棕色-布局背景(来自可绘制) 以下是我如何设置父布局背景:Android 如何根据父布局内容高度动态调整图像高度?,android,android-layout,android-drawable,background-drawable,Android,Android Layout,Android Drawable,Background Drawable,我有一个android:layout\u height=“wrap\u content”的父布局,在我的类代码中,它应该设置特定图像的布局背景,但图像高度大于内容高度。我想将背景的图像高度调整为与父布局内容高度相同的大小。我该怎么做 黑色-父布局 绿色-布局内容 棕色-布局背景(来自可绘制) 以下是我如何设置父布局背景: view.setBackgroundDrawable(getResources().getDrawable(R.drawable.brown)) 编辑: 以下是我的xml:
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.brown))代码>
编辑:
以下是我的xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/item_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:src="@android:color/transparent"
android:translationX="-10dp" />
<LinearLayout
android:id="@+id/item_song"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#b97a57"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_weight="0"
android:background="@color/aqua"
android:padding="1dip" >
<ImageView
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@color/gray" />
<!-- Thumbnail Progressbar -->
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout></FrameLayout>
我在java类侦听器中调用此布局的位置:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_item_selected));
}
@覆盖
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_item_selected));
}
这就是我的意思,我无法获取ImageView本身的引用,因为我只能从onItemClick
获取视图。这是一个列表视图,我想在单击时更改背景
无论如何,我如何调整内容的高度,使背景(棕色)高度刚好填充父布局(绿色)?如果您设置父布局的背景,它将覆盖父布局。但是,您希望棕色只覆盖绿色部分,对吗?发布布局xml文件。是的。实际上这是一个listview项目布局,顺便说一句,我的父布局是FrameLayout,其中一个子布局是ImageView,我想专门更改ImageView的背景,但我不能在代码中这样做,因为view.setBackgroundDrawable(getResources().getDrawable(R.drawable.brown))
在@Override public void onItemClick(AdapterView父对象、视图视图、int位置、长id){
侦听器中。我不知道如何在视图
对象中获取ImageView。但不知道这一点,您有没有办法将brown动态调整为geen高度?您的确切意思是什么“我不知道如何在视图对象内部获取ImageView”。如果要将背景设置为此ImageView
,只需使用ImageView
变量并使用setBackgroundDrawable()
方法
。您可能必须将ImageView
变量声明为final
(通过在声明前添加final)。是否可以将布局xml?