2016-07-31 18 views
0

私は動的なグリッドビューをプログラマティックに作成したいと思います。すべてのものが良く見えます。しかし、各セルのマージンは表示されません。助けてください。各セルの余白をプログラムで設定してGridViewを作成

PagerAdapter:

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    Calendar viewData = getData(position); 
    GridView gridView = new GridView(mContext); 
    gridView.setLayoutParams(new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.MATCH_PARENT)); 
    gridAdapter = new GridAdapter(mContext, viewData); 
    gridView.setNumColumns(4); 
    gridView.setAdapter(gridAdapter); 
    container.addView(view); 
    return gridView; 
} 

GridAdapter

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.cal_single_cell, parent, false); 
    layout.setLayoutParams(new GridView.LayoutParams(2, 2)); 
    TextView lblDisplay = (TextView) layout.findViewById(R.id.lbl_cell_name); 
    lblDisplay.setText(getDisplayText(position)); 

    if(CalendarConverter.dateCompare(getData(position), Calendar.getInstance(), VIEW_LEVEL) == 0){ 
     layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.backgorund_current_cell)); 
    } 
    return layout; 
} 

そして、私のcal_single_cell.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content" android:theme="@style/CalendarSingleCell"> 

    <TextView 
     android:id="@+id/lbl_cell_name" 
     android:text="Jan" 
     android:textSize="18dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="64dp" 
     android:gravity="center" /> 

</LinearLayout> 

view display result on google drive

+0

こんにちは@グエンダン、私はあなたの問題に答えましたが、回答はありません、私の答えがあなたを助けたかどうかの情報はありません。もし私の答えが正しい答えを設定してくださいしてください。ありがとう。 –

+0

こんにちはMaciej Sikora、私の言うことはまだ私のために働いていません: ViewGroup layout =(ViewGroup)inflater.inflate(R.layout.cal_single_cell、parent、false);layout.setLayoutParams(new GridView.LayoutParams(2、2)); 私は白いページを持っています。 ViewHolderはパフォーマンスには本当に良いと思う。ありがとうございました。 –

+0

ペーストあなたの現在のコードを、私はあなたに役立つでしょう、私はあなたがそれをすべての時間を間違っていることをコメントで見る。 –

答えて

0

私はあなたの主なミストを考えますあなたはセルマージンを変更したいが、この変更GridViewマージンの代わりに、

GridViewの属性android:horizo​​ntalSpacing,android:verticalSpacingを使用してください。この質問の詳細 - Increase the grid spacing in android

または、マージンを変更して、ビューの内側の行ビュー(すべてのセルビュー)を変更します。あなたのコードでは

view.setLayoutParams(/* your layout params */); //where view is cell view 

:ProgramicallyアダプタgetViewメソッド方法でそのような何かを行う必要ならば、すべてのgetViewメソッドにおけるレイアウトパラメータを変更する

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = LayoutInflater.from(mContext); 
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.cal_single_cell, parent, false); 

    //**************************** 
    //here set padding or margin 

    layout.setLayoutParams(/* your layout params */); 

    //*************************** 
    TextView lblDisplay = (TextView) layout.findViewById(R.id.lbl_cell_name); 
    lblDisplay.setText(getDisplayText(position)); 

    if(CalendarConverter.dateCompare(getData(position), Calendar.getInstance(), VIEW_LEVEL) == 0){ 
     layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.backgorund_current_cell)); 
    } 
    return layout; 
} 

重要

は、非常に最適ではありませんそれを実行してViewHolderを作成する必要があります。

+0

こんにちは@Maciej Sikora、ご協力ありがとうございます 私はGridView.LayoutParamsを私の更新された投稿のGridViewの各セルに追加します。私はあなたに十分な情報を願っています。どうもありがとうございました。 –

関連する問題