私は基本的に完了しているアプリに取り組んでいますが、コードを見直して最適化しています。インフレートの「親」部分を使用して、レイアウトパラメーターを保持できるようにするのが適切であると述べているインフレータブルのガイドを見つけました。これは、元のバージョンでは実行していませんでした。
では問題に移ります。View を組み込むようにコードを変更しましたが (以下を参照)、レイアウト パラメータは追加されていないため、プログラムで追加する必要があります。
元のコード、これは機能しますが、layoutparams を設定する必要があることに満足していません (読みやすくするためにコードを少し短くしていることに注意してください。関心のあるものは何も削除していません)。
// this is the parent
LinearLayout llCategory = (LinearLayout) findViewById(R.id.llCategory);
categoryArrayList = db.getCategory(); // this gets the data for me.
// Add the Categories
for (int i = 0; i < categoryArrayList.size(); i++) {
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// This is my original inflate
//View view = layoutInflater.inflate(R.layout.row_standard, null);
// this is the inflate I would like to use.
View view = layoutInflater.inflate(R.layout.row_standard, llCategory, false);
LinearLayout llCard = (LinearLayout) view.findViewById(R.id.llStandardCard);
// here is some code where I change the background of llCard - which is the base layout for the view I inflated above.
// this is where I have to set the paddings programmatically on the view (or rather the linear layout that the view contains)
// get the right paddings for the card and set them.
int standardPadding = (int)getResources().getDimension(R.dimen.standard_padding);
int bottomPadding = (int)getResources().getDimension(R.dimen.standard_card_padding_bottom);
llCard.setPadding(standardPadding, standardPadding,standardPadding,bottomPadding);
// Add the complete view to the layout.
llCategory.addView(view);
}
要約すると。このバージョンのインフレータブルを使用するかどうかにかかわらず: View view = layoutInflater.inflate(R.layout.row_standard, null);
または、このバージョンのインフレートを使用する場合: View view = layoutInflater.inflate(R.layout.row_standard, llCategory, false);
プログラムでパディング(XMLですでに設定されている)を設定する必要があります-何が間違っていますか。
編集、XML の追加:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/llStandardCard"
style="@style/StandardCardStyle">
<TextView
android:id="@+id/tvStandardRowText"
style="@style/RowText"/>
</LinearLayout>
</LinearLayout>
また、スタイル:
<style name="StandardCardStyle" parent="@style/AbstractCard">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginTop">@dimen/standard_half_padding</item>
<!--Very custom paddings due to the background messing with the standard ones-->
<item name="android:paddingLeft">@dimen/standard_padding</item>
<item name="android:paddingRight">@dimen/standard_padding</item>
<item name="android:paddingTop">@dimen/standard_padding</item>
<item name="android:paddingBottom">@dimen/standard_card_padding_bottom</item>
<item name="android:layout_gravity">top</item>
<item name="android:clickable">true</item>
</style>