0

我的基础布局是LinearLayouts的ListView,网格中的每一项都是下面的FrameLayout。我正在将图像下载到 ImageView。在加载图像之前,没有内容并且布局缩小到小 ProgressBar 的高度(第一个问题),当我预计它是 240dp 或 120dp 时。图像放入视图后,布局没有调整(第二个问题),高度仍然是一个小进度条的缩小尺寸。

加载图像代码:

@Override
public View getView(View convertView) {
    // ...
    // set up holder
    // ...
    new GetPhotoTask().execute(holder.my_tile_image, holder.my_loading, my_media_url);
    holder.my_tile_image.setVisibility(View.INVISIBLE);
    holder.my_loading.setVisibility(View.VISIBLE);
    holder.my_tile_label.setText(activityObject1.track.name);
    // ...
}


private final class GetPhotoTask extends AsyncTask<Object, Void, Drawable> {

    ImageView iv;
    ProgressBar pb;

    @Override
    protected Drawable doInBackground(Object... params) {
        iv = (ImageView) params[0];
        pb = (ProgressBar) params[1];
        return new BitmapDrawable(getResources(), loadAsset(params[2]).media_url, true));
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        pb.setVisibility(View.GONE);
        iv.setImageDrawable(result);
        iv.setVisibility(View.VISIBLE);
    }
}

主要的xml:

<ListView
        android:id="@+id/my_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="2dp" />

每行的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_row_type_3"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:weightSum="3" >
    <include layout="@layout/my_tile_layout"
        android:id="@+id/my_tile_1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />
    <LinearLayout
        android:id="@+id/my_row_type_2"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:weightSum="2" >
        <include layout="@layout/my_tile_layout"
            android:id="@+id/my_tile_2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
        <include layout="@layout/my_tile_layout"
            android:id="@+id/my_tile_3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

行内容的 xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_tile_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="2dp" >
    <ImageView
        android:id="@+id/my_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:contentDescription="@string/foo" />
    <ProgressBar
        android:id="@+id/my_loading"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" />
    <TextView
        android:id="@+id/my_tile_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_title"
        android:layout_gravity="left|bottom" />
</FrameLayout>

为什么即使我为每行硬编码 240dp,初始布局也会缩小到进度条的高度?为什么在 ImageView 内容中设置位图也不会导致它在此时调整大小?第一个问题最重要,因为我相信它会使第二个问题无效。提前致谢。

解决方案

为了获得我想要的结构,我将 RelativeLayout 用于图块(用于分层能力),并且对于每一行,我将覆盖 LinearLayout 以强制为每一行设置我想要的大小。由于每个图块的内容都可能大于或小于视图,这破坏了我依赖 centerCrop 或 fitCenter 的能力。所以在我的自定义布局中,我将高度设置为与每行的宽度成正比,并将 MeasureSpec 的模式设置为 EXACTLY。

public final class MyRow3 extends LinearLayout { 
// ... 
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, makeMeasureSpec(getSize(widthMeasureSpec) * 2 / 3, MeasureSpec.EXACTLY));
}
4

2 回答 2

0

我不确定为什么它不尊重它,但为什么不这样做:

在 ImageView 中设置绝对高度以保证一些空间。

<ImageView
    android:layout_height="###dp"
/>

然后在加载图像之前将布局更改回自动调整状态:

iv.setLayoutParams( new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT ) );
于 2013-04-25T15:56:07.447 回答
0

为了获得我想要的结构,我将 RelativeLayout 用于图块(用于分层能力),并且对于每一行,我将覆盖 LinearLayout 以强制为每一行设置我想要的大小。由于每个图块的内容都可能大于或小于视图,这破坏了我依赖 centerCrop 或 fitCenter 的能力。所以在我的自定义布局中,我将高度设置为与每行的宽度成正比,并将 MeasureSpec 的模式设置为 EXACTLY。

public final class MyRow3 extends LinearLayout { 
// ... 
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, makeMeasureSpec(getSize(widthMeasureSpec) * 2 / 3, MeasureSpec.EXACTLY));
}
于 2013-05-15T23:13:15.567 回答