76

この質問の最後に、コメントと解決策を組み合わせた回答が記入されています。

質問

私は周りを検索しましたが、Android LintといくつかのEclipseのヒントがいくつかのlayout_heightと のlayout_width値を0dp.

たとえば、ListView変更が提案された

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
</ListView>

同様に、ListView itemへの変更を提案しました。これらはすべて変更の前後で同じように見えますが、なぜこれらがパフォーマンス ブースターなのかを理解したいと思っています。

誰にも理由の説明がありますか?それが役立つ場合、ここに を使用した一般的なレイアウトがありListViewます。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/logo_splash"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ImageView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@color/background"
        android:layout_below="@id/logo_splash">

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </ListView>

        <TextView
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no_upcoming" />

    </LinearLayout>        
</RelativeLayout>

答え

実際には回答と以下の参照リンクの組み合わせであるため、ここに回答を入れています。私が何か間違っている場合は、私に知らせてください。

から0dip layout_height または layouth_width のトリックは何ですか?

高さで機能する 3 つの一般的なレイアウト属性があります。

  1. android:layout_height
  2. android:layout_width
  3. android:layout_weight

aLinearLayoutverticalの場合、 は子s ( ) の高さlayout_weightに影響します。を に設定すると、この属性が無視されます。ViewListViewlayout_height0dp

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </ListView>
</LinearLayout>

aLinearLayouthorizo​​ntalの場合、 は子s ( ) のlayout_weightに影響します。を に設定すると、この属性が無視されます。ViewListViewlayout_width0dp

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <ListView
        android:id="@android:id/list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </ListView>
</LinearLayout>

この属性を無視する理由は、無視しないと、より多くの CPU 時間を使用するレイアウトの計算に使用されるためです。

さらに、これにより、3 つの属性を組み合わせて使用​​する場合にレイアウトがどのように見えるかについての混乱を防ぐことができます。これは、以下の回答で@android 開発者によって強調されています。

また、Android LintEclipseはどちらも を使用すると言っています0dip0dip以下の回答から、0dp、 、 などを使用できます0px。ゼロのサイズはどの単位でも同じだからです。

ListView で wrap_content を避ける

ListView の Layout_widthから

getView(...)がなぜ私のように何度も呼び出されるのか疑問に思ったことがあるなら、それは に関連していることがわかりwrap_contentます。

上記で使用してwrap_contentいたように使用すると、すべての子Viewが測定され、CPU 時間がさらに長くなります。この測定により、あなたgetView(...)が呼び出されます。これをテストしたところ、呼び出される回数getView(...)が劇的に減少しました。

私がwrap_content2つListViewのsで使用していたとき、1つのgetView(...)行ごとに3回、もう1つの行ListViewごとに4回呼び出されました。

これを推奨の に変更すると、0dpgetView(...)ごとに 1 回だけ呼び出されました。これはかなりの改善ですwrap_contentListView0dp.

ただし、このため、の提案により0dpパフォーマンスが大幅に向上します。

4

5 に答える 5

24

まずこれを持って、

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>

ListView の高さを wrap_content として使用しないでください。問題が発生します。Hereがその理由ですthis answer

さらに、

私は周りを検索しましたが、Android Lint といくつかの Eclipse ヒントがいくつかの layout_height と layout_width の値を 0dp に置き換えることを提案する理由を実際に説明するものは見つかりませんでした。

layout_weight = "1"それは、ListView が使用可能な限りの高さを使用していることを意味します。したがって、その場合は使用する必要はなく、layout_height = "wrap_content"に変更するだけでandroid:layout_height="0dp"、ListView の高さは によって管理されlayout_weight = "1"ます。

于 2012-08-18T07:53:19.413 に答える
12

そのため、ビュー X で android:layout_weight が使用され、LinearLayout が水平の場合、X の android:layout_width は単純に無視されます。

同様に、ビュー X で android:layout_weight が使用され、LinearLayout が垂直の場合、X の android:layout_height は無視されます。

これは実際には、無視されたフィールド (0dp、fill_parent、または wrap_content) に何でも入力できることを意味します。それは問題ではありません。ただし、ビューが高さや幅の余分な計算を行わないように 0dp を使用することをお勧めします (無視されます)。この小さなトリックは、CPU サイクルを単純に節約します。

から :

0dip layout_height または layouth_width のトリックは何ですか?

于 2012-08-18T07:50:17.407 に答える
6

私の知る限り、0dp (または 0px ところで、ここの単位が何であっても 0 は 0 であるため同じです) と wrap_content または fill_parent (または match_parent、それは同じです) の使用には違いがあります。

使用する重量によって異なります。weight of 1 のみを使用すると、それらはすべて同じように見えますが、意味は常に異なり、パフォーマンスにとって重要です。

これを表示するには、次のことを試してください。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/textView1" android:layout_width="match_parent"
    android:layout_height="0px" android:text="1" android:background="#ffff0000"
    android:layout_weight="1" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

  <TextView android:id="@+id/textView2" android:layout_width="match_parent"
    android:layout_height="0px" android:text="2" android:background="#ff00ff00"
    android:layout_weight="2" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

  <TextView android:id="@+id/textView3" android:layout_width="match_parent"
    android:layout_height="0px" android:text="3" android:background="#ff0000ff"
    android:layout_weight="3" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

</LinearLayout>

次に、 0px を match_parent に置き換えてみてください。結果が大きく異なることがわかります。

通常、理解を深め、パフォーマンスを向上させるために、0px を使用することをお勧めします。

于 2012-08-18T08:40:59.623 に答える
3

LinearLayoutlayout_width/値に従ってすべての子を測定し、値に従ってlayout_height残りのスペース (負の可能性があります) を分割しlayout_weightます。

0dpwrap_content最初に子を測定してから残りを体重に基づいて分割するよりも、元の高さにゼロを使用してから、体重に基づいて親の全高を分割する方が効率的であるため、この場合よりも効率的です。

したがって、効率は子供を測定しないことから来ます。、または、またはその他の固定数0dpとまったく同じくらい効率的である(そしてまったく同じ結果を生成する)必要があります。match_parent42px

于 2014-03-20T06:40:53.063 に答える
1

android:layout_height="0dp" の使用に関する注意

ListView で (convertView を使用した推奨の View リサイクルを使用して、http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ などを参照)、設定 android:layout_height="行 TextView の 0dp" は、複数行のテキスト コンテンツのテキストの切り捨てにつながる可能性があります。

1 行に収まるテキストを表示するために以前に使用された TextView オブジェクトが、複数行を必要とする長いテキストを表示するために再利用される場合は常に、そのテキストは 1 行に切り捨てられます。

問題は android:layout_height="wrap_content" を使用することで解決されます

于 2014-04-15T14:01:27.237 に答える