私はアダプタから4つの画像ビューを取得するgridviewを持っています。私は2x2のサイズを変更して画面に合わせたいと思っています。2x2 imageviewのGridlayout android
here'sそのためコード:瞬間
@Override
protected View createQuizContentView() {
mAnswerView = new GridView(getContext());
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(Color.LTGRAY));
selector.addState(new int[]{-android.R.attr.state_pressed}, new ColorDrawable(Color.WHITE));
mAnswerView.setSelector(selector);
//mAnswerView.setSelector(R.drawable.selector_button);
mAnswerView.setNumColumns(2);
mAnswerView.setAdapter(new OptionsQuizAdapter(getQuiz().getOptions(),
R.layout.imgitem_answer));
mAnswerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mAnswered = position;
allowAnswers(mAnswerView,position);
}
});
return mAnswerView;
}
と
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(mLayoutId, parent, false);
}
final ImageView imageView;
String text = getText(position);
Context context = parent.getContext();
Resources resources = context.getResources();
String stripped = text.replace("-", "");
final int resourceId = resources.getIdentifier(stripped, "drawable",
context.getPackageName());
Drawable d = ContextCompat.getDrawable(context, resourceId);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
double dviewheight = height/3.32;
int viewheight = (int)dviewheight;
imageView = (ImageView) convertView;
imageView.setPadding(64, 64, 64, 64);
convertView.setLayoutParams(newGridView.LayoutParams(GridView.AUTO_FIT, viewheight));
imageView.setImageDrawable(d);
return imageView;
}
の画面サイズを取得し、それを分割してGridViewの内部ImageViewの高さを設定i'amほとんどのデバイスの解像度で正常に動作しますが、グリッドビューにさらに多くの行を追加すると機能するよりダイナミックなソリューションが好まれます。
これは、それがどのように見えるかです:
私は高さにGridView.AUTO_FITを使用する場合、それは無駄なスペースの多くを追加し、私はこのような右のスクロールバーを取得:
私はいくつかの設定とストレッチを試しましたが、どれもうまくいかないようです。任意の方法は、私はxmlなしでプログラムで欲しいものを達成するために?
'xmlなしでプログラム的にしたいものを実現する方法は何ですか?そして**どうして**あなたはそれをやりたいのですか**難しい方法**? –
APIレベル21以上を使用している場合、すべての別々の 'views'の' weight'を1に設定しようとします。これは 'GridLayout'の幅と高さをすべて分配します。 – Marcel50506