2016-07-08 5 views
0

화면 중간에 gridview를 가운데에 배치하는 데 문제가 있습니다. 오른쪽 하단은 왼쪽보다 더 많은 여백이 있습니다. gridview를 상대적인 위치에 놓고 중심에 두는 것을 포함하여 모든 것을 시도했습니다.화면 중앙에 거치 뷰를 중앙에 배치하는 방법은 무엇입니까?

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

나는 이미 gridView를 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

관련 문제