この質問の最後に、コメントと解決策を組み合わせた回答が記入されています。
質問
私は周りを検索しましたが、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 つの一般的なレイアウト属性があります。
android:layout_height
android:layout_width
android:layout_weight
aLinearLayout
がverticalの場合、 は子s ( ) の高さlayout_weight
に影響します。を に設定すると、この属性が無視されます。View
ListView
layout_height
0dp
例
<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>
aLinearLayout
がhorizontalの場合、 は子s ( ) の幅layout_weight
に影響します。を に設定すると、この属性が無視されます。View
ListView
layout_width
0dp
例
<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 LintとEclipseはどちらも を使用すると言っています0dip
。0dip
以下の回答から、0dp
、 、 などを使用できます0px
。ゼロのサイズはどの単位でも同じだからです。
ListView で wrap_content を避ける
getView(...)
がなぜ私のように何度も呼び出されるのか疑問に思ったことがあるなら、それは に関連していることがわかりwrap_content
ます。
上記で使用してwrap_content
いたように使用すると、すべての子View
が測定され、CPU 時間がさらに長くなります。この測定により、あなたgetView(...)
が呼び出されます。これをテストしたところ、呼び出される回数getView(...)
が劇的に減少しました。
私がwrap_content
2つListView
のsで使用していたとき、1つのgetView(...)
行ごとに3回、もう1つの行ListView
ごとに4回呼び出されました。
これを推奨の に変更すると、0dp
行getView(...)
ごとに 1 回だけ呼び出されました。これはかなりの改善ですwrap_content
がListView
、0dp
.
ただし、このため、の提案により0dp
パフォーマンスが大幅に向上します。