2016-07-08 20 views
0

画面の中央にグリッドビューのセンタリングに問題があります。今すぐ右下にあるスクリーンショットのように見えます。右端には左端よりパディングが多いことに注意してください。グリッドビューを相対的なレイアウトに配置して中心に置くことを含め、すべてを試しました。画面の中央にガードビューの中心を合わせるにはどうすればいいですか?

activity_main.xml問題の

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="90dp" 
       android:gravity="center" 
       android:orientation="vertical" 
       android:minHeight="90dp" 
       android:padding="16dp" 
       android:background="#EEEEEE"> 



     <TextView 
      android:id="@+id/text_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      tools:text="test text" 
      android:maxLines="2" 
      /> 


</LinearLayout> 

スクリーンショット

<?xml version="1.0" encoding="utf-8"?> 

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/gridview" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:columnWidth="90dp" 
      android:numColumns="auto_fit" 
      android:verticalSpacing="16dp" 
      android:horizontalSpacing="16dp" 
      android:stretchMode="columnWidth" 
      android:longClickable="true" 
      android:gravity="center" 
      android:layout_margin="16dp" 
    /> 

grid_item.xml:あなたは右に空白がたくさんある見ることができるように。 enter image description here

答えて

0
 **Please update your activity_main.xml** 

     <?xml version="1.0" encoding="utf-8"?> 
      <GridView android:layout_width="wrap_content" 
       android:id="@+id/grid1" 
       android:layout_height="match_parent" 
       android:columnWidth="130dp" 
       android:numColumns="auto_fit" 
       android:verticalSpacing="16dp" 
       android:horizontalSpacing="16dp" 
       android:stretchMode="columnWidth" 
       android:layout_centerInParent="true" 
       android:background="#000000" 
       android:longClickable="true" 
       android:gravity="center" 
       xmlns:android="http://schemas.android.com/apk/res/android"/> 


    **grid_item.xml** 
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="90dp" 
       android:gravity="center" 
       android:orientation="vertical" 
       android:minHeight="90dp" 
       android:padding="16dp" 
       android:background="#EEEEEE"> 



       <TextView 
        android:id="@+id/txtEmpName" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        tools:text="test text" 
        android:maxLines="2" 
        /> 


      </LinearLayout> 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     GridViewAdapter gridViewAdapter = new GridViewAdapter(MainActivity.this); 
     GridView gridView = (GridView) findViewById(R.id.grid1); 
     gridView.setAdapter(gridViewAdapter); 
    } 
} 

public class GridViewAdapter extends BaseAdapter { 
    String [] empName = {"Krunal Patel","user11230","Alpa chauhan","loadjang","Aditya Vyas"}; 
    Activity context; 
    public GridViewAdapter(Activity context) { 
     this.context = context; 

    } 

    @Override 
    public int getCount() { 
     return empName.length; 
    } 

    @Override 
    public Object getItem(int i) { 
     return empName[i]; 
    } 

    @Override 
    public long getItemId(int i) { 
     return i; 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     LayoutInflater inflat = context.getLayoutInflater(); 

     Holder holder; 
     if (view == null) { 
      view = inflat.inflate(R.layout.grid_adapter_layout, null, false); 
      holder = new Holder(); 
      holder.txtEmpName = (TextView) view.findViewById(R.id.txtEmpName); 
      view.setTag(holder); 
     } else { 
      holder = (Holder) view.getTag(); 
     } 
     holder.txtEmpName.setText(empName[i]); 

     return view; 
    } 

    class Holder { 
     TextView txtEmpName; 
    } 
} 

[![enter image description here][1]][1] 


    [1]: http://i.stack.imgur.com/yvslu.png 
+0

ありがとうございますが、3列をハードコーディングしたくない場合はどうすればよいですか?たとえば、タブレットでは、できるだけ多くの列をすべての画面に表示したいと考えています。 – user11230

+0

grideViewにandroid:layout_centerInParent = "true"を追加してください。 –

+0

私はすでにそれを試しましたが、うまくいきません。 – user11230

0

RelativeLayout子ビューのアンドロイド:layout_centerInParent = "true" を使用

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="90dp"> 
    <TextView 
     android:id="@+id/text_1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="text" 
     android:maxLines="2" 
     /> 

</RelativeLayout> 
+0

私はすでに、 relativeLayout、次にcenterInParent = "true"をgridVIewで使用しても動作しませんでした。 – user11230

0
If you want to keep same space then you have to set dynamic property of gridview like this. 

    /** 
    * Set gridView data according to device width. 
    */ 
    private void InitilizeGridLayout() { 
     Resources r = getResources(); 
     float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, r.getDisplayMetrics()); 

     columnWidth = (int) ((getScreenWidth() - ((3 + 1) * padding))/3); 

     gvGallery.setNumColumns(3); 
     gvGallery.setColumnWidth(columnWidth); 
     gvGallery.setStretchMode(GridView.NO_STRETCH); 
     gvGallery.setPadding((int) padding, (int) padding, (int) padding, (int) padding); 
     gvGallery.setHorizontalSpacing((int) padding); 
     gvGallery.setVerticalSpacing((int) padding); 
    } 
+0

もし私が3列だけをハードコアしたくないのであれば? – user11230

+0

そのため、列の幅を修正する必要があります。 – Alpha

+0

申し訳ありませんが、あなたのコードで何を修正する必要がありますか?そしていつInitilizeGridLayout()メソッドを呼び出さなければなりませんか? – user11230

0

..あなたはRelativeLayout内のGridViewを配置する必要があり、この方法を試してみて、ちょうどtrueにGridViewのlayout_centerInParentを設定
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <GridView 
      android:id="@+id/grid" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:numColumns="7" 
      android:columnWidth="40dp" 
      android:verticalSpacing="1dp" 
      android:horizontalSpacing="1dp" 
      android:paddingTop="2dp" 
      android:stretchMode="columnWidth" 
      android:background="#696969" 
      android:layout_centerInParent="true" /> 

</RelativeLayout> 
+0

申し訳ありませんが、動作しません。 – user11230

関連する問題