イメージビューを画面に均等に分散しようとしています。イメージビューのサイズは異なります。以下の例では、各行に 4 つ、3 行の 12 個のイメージビューが必要です。私が得る問題は、ちょうど左にまとまり、行全体を塗りつぶそうとするたびに、それらがちょうど伸びているということですが、代わりにイメージビューの間にスペースが必要です。画面に表示するイメージビューの数を選択したいので、これをプログラムで実行したいと考えています。イメージビューを配置したいlinearLayoutだけを含むレイアウトがあります:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/treasureLinearLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff134BE8"
android:padding="10dp"
/>
イメージビューを追加するコードはフラグメントにあります。
final float scale = getContext().getResources().getDisplayMetrics().density;
int dps = 90;
LinearLayout layout = (LinearLayout) view.findViewById(R.id.treasureLinearLayout);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(getContext());
row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
row.setLayoutParams(param);
for (int j = 0; j < 4; j++) {
dps -= 5;
ImageView imageView = new ImageView(getContext());
imageView.setImageResource(R.drawable.ic_menu_map);
imageView.setBackgroundResource(R.drawable.circle);
imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
int pixels = (int) (dps * scale + 0.5f);
imageView.getLayoutParams().height = pixels;
imageView.getLayoutParams().width = pixels;
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
row.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TreasuresFragment treasureFrag = ((NavigationDrawerActivity) getActivity()).getTreasureFrag();
((NavigationDrawerActivity)getActivity()).showHideFragment(treasureFrag);
}
});
}
layout.addView(row);
}