5

現在、プロジェクトの小さな更新に取り組んでいますが、リストビューのRelative_Layoutとfill_parentに問題があります。デフォルトのダイヤラのコールログの仕切りのように、各行の2つのセクションの間に仕切りを挿入しようとしています。Androidのソースコードをチェックして、どのように実行されたかを確認しましたが、ソリューションを複製するときに問題が発生しました。

まず、行アイテムのレイアウトを次に示します。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
 android:layout_width="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:padding="10dip" 
 android:layout_height="fill_parent" 
 android:maxHeight="64dip" 
 android:minHeight="?android:attr/listPreferredItemHeight">
 <ImageView android:id="@+id/infoimage" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:clickable="true"
  android:src="@drawable/info_icon_big" 
  android:layout_alignParentRight="true" 
  android:layout_centerVertical="true"/>
 <View android:id="@+id/divider" 
  android:background="@drawable/divider_vertical_dark" 
  android:layout_marginLeft="11dip" 
  android:layout_toLeftOf="@+id/infoimage" 
  android:layout_width="1px" 
  android:layout_height="fill_parent"
  android:layout_marginTop="5dip" 
  android:layout_marginBottom="5dip" 
  android:layout_marginRight="4dip"/>
 <TextView android:id="@+id/TextView01" 
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:layout_centerVertical="true" 
  android:layout_toRightOf="@+id/ImageView01" 
  android:layout_toLeftOf="@+id/divider"
  android:gravity="left|center_vertical" 
  android:layout_marginLeft="4dip" 
  android:layout_marginRight="4dip"/>
 <ImageView android:id="@+id/ImageView01" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignParentLeft="true" 
  android:background="@drawable/bborder" 
  android:layout_centerVertical="true"/>
</RelativeLayout>

私が直面している問題は、各行にさまざまな高さのサムネイル(ImageView01)があることです。RelativeLayoutのlayout_heightプロパティをfill_parentに設定すると、仕切りは行を埋めるために垂直方向に拡大縮小されません(1pxのドットのままです)。layout_heightを"?android:attr / listPreferredItemHeight"に設定すると、仕切りは行を埋めますが、サムネイルは縮小します。アダプタのgetView()メソッドでデバッグを行いましたが、行が適切な高さになると、仕切りの高さが適切に設定されていないようです。

getView()メソッドの一部を次に示します。

public View getView(int position, View view, ViewGroup parent) {
            if (view == null) {
                view = inflater.inflate(R.layout.tag_list_item, parent, false);
            }

メソッドの残りの部分では、行に適切なテキストと画像を設定するだけです。

また、アダプターのコンストラクターでインフレーターオブジェクトを次のように作成します。inflater = LayoutInflater.from(context);

私は何か重要なものが欠けていますか?または、fill_parentは動的な高さでは機能しませんか?

4

3 に答える 3

6

他の誰かがこの問題に遭遇した場合、私が見つけた唯一の方法 (固定高さ以外) は、LinearLayout に切り替えることです。これは、LinearLayouts と RelativeLayouts の描画方法の根本的な違いによるものだと思います。Romain Guy がこの記事でそれについて語っています。LinearLayouts は onMeasure() を幅に対して 1 回、高さに対して 1 回呼び出しますが、RelativeLayouts はそうではありません。そのため、他のすべてがレイアウトされると、戻ってプロッパーの高さまで垂直の仕切りを埋める方法がありません。解決策は次のとおりです。

<LinearLayout xmlns:...
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <RelativeLayout android:id="@+id/leftContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >
    </RelativeLayout>
    <View android:id="@+id/divider"
        android:layout_width="1px"
        android:layout_height="fill_parent"
        android:background="@drawable/divider" />
    <RelativeLayout android:id="@+id/rightContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >
    </RelativeLayout>
</LinearLayout>

これは、RelativeLayout だけほど効率的ではありませんが、目的の結果が得られるはずです。

于 2011-08-01T18:45:53.860 に答える
0

興味のある方はどうぞ。これは、SDK のバグである可能性がある (またはあった) ようです。この問題を解決するには、行の高さを固定する必要がありました。これが最終的なレイアウト コードです。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="100dip" android:layout_width="fill_parent">
    <RelativeLayout android:id="@+id/Thumbnail"
        android:layout_alignParentLeft="true" android:paddingLeft="10dip"
        android:paddingTop="10dip" android:paddingRight="15dip"
        android:paddingBottom="10dip" android:layout_height="fill_parent"
        android:layout_width="wrap_content">
        <ImageView android:id="@+id/image"
            android:layout_width="80dip" android:background="@drawable/bborder"
            android:layout_centerVertical="true" android:adjustViewBounds="true"
            android:layout_height="80dp" android:cropToPadding="true" />
    </RelativeLayout>
    <View android:id="@+id/divider" android:background="@drawable/divider_light"
        android:layout_toRightOf="@+id/Thumbnail" android:layout_marginTop="10dip"
        android:layout_marginBottom="10dip" android:layout_width="1px"
        android:layout_height="fill_parent" android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" />
    <LinearLayout 
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:paddingTop="10dp" android:paddingBottom="10dp" 
        android:layout_centerVertical="true" android:orientation="vertical"
        android:layout_marginLeft="20dip" android:layout_marginRight="10dip"
        android:layout_toRightOf="@+id/divider">
        <TextView android:id="@+id/title" android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:gravity="left|center_vertical" android:maxLines="1" android:singleLine="true"
            android:ellipsize="end" />
        <TextView android:id="@+id/science_name" android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:gravity="left|center_vertical" android:maxLines="1" android:singleLine="true"
            android:ellipsize="end" android:textStyle="italic" />
    </LinearLayout>
</RelativeLayout>
于 2010-12-08T23:17:33.810 に答える
0

レイアウトに循環依存関係があると思われます。行layout_heightを にしてfill_parentよろしいですか? @id/ImageView01行を最大のサブビュー (この場合)と同じ高さにしたいように聞こえるのでlayout_height="wrap_content"RelativeLayout. このようにして、高さは画像から行に伝播し、次に を持つ他の子に伝播しますlayout_height="fill_parent"

于 2010-05-01T20:51:20.517 に答える