2017-03-08 2 views
0

다음과 같은 레이아웃을 만들고 싶습니다. https://gyazo.com/2a08bcd731fb1cfeb07e72f75bc05e7e 모든 장치에 맞는 것은 무엇입니까?어떻게 이렇게 레이아웃을 프로그래밍 할 수 있습니까?

화면은 내 것이지만 모든 화면 크기에 맞지 않는 문제가 있습니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <GridLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_columnWeight="1" 
       android:layout_rowWeight="1" 
       android:layout_column="0" 
       android:layout_row="0" 
       android:text="Button"/> 

      <Button 
       android:id="@+id/Button2" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_columnWeight="1" 
       android:layout_rowWeight="1" 
       android:layout_column="1" 
       android:layout_row="0" 
       android:text="Button"/> 

      <Button 
       android:id="@+id/Button3" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_columnWeight="1" 
       android:layout_rowWeight="1" 
       android:layout_column="0" 
       android:layout_row="1" 
       android:text="Button"/> 

      <Button 
       android:id="@+id/Button4" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_rowWeight="1" 
       android:layout_columnWeight="1" 
       android:layout_column="1" 
       android:layout_row="1" 
       android:text="Button"/> 

       //and 200 buttons more 

    </GridLayout> 

</ScrollView> 

어떻게 모든 화면 크기에 맞는이 같은 레이아웃을 programm 수 있습니다 여기에 https://gyazo.com/84d878061234c25ae872d4ed2491f2f4

을 나는이 레이아웃을 pragramm하는 데 사용되는 코드는 다음과 같습니다 일부 장치에 그것은 다음과 같습니다? 중요 마음에 온있는 ScrollView

+0

사용자 정의 어댑터를 사용하여'Gride view' 또는'RecyclerView'의'GridAdapter'를 검색하십시오. –

+0

활동에 코드를 작성하지 않고도 가능합니까? 버튼의 배열 만 필요합니다. – DaFaack

+0

당신이 무엇을 의미하는지 확신 할 수 없지만'ListView'에서하는 것과 같이'GridAdapter'를 사용할 수 있으며'RecyclerView' AFAIK를 사용하여 얻을 수 있습니다! –

답변

1

첫 번째 생각은 당신에게 2 열 수직으로 추락 목록을 줄 것이다 GridLayoutManager

GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2, LinearLayoutManager.VERTICAL, false); 
yourRecyclerView.setLayoutManager(gridLayoutManager); 

이 함께 RecyclerView을 사용하는 것입니다. 마음에 들지 않으면 CardView도 체크하고 싶을 때가 있지만, 때로는 함께 사용하는 것을 좋아합니다.

RecyclerView 용 어댑터를 작성하고 나면 활동에 GridLayoutManager 및 YourAdapter를 설정할 수 있으며 이는 실제로 액티비티에 필요한 유일한 것입니다 : 어댑터 초기화, 레이아웃 관리자 및 어댑터 설정 귀하의 recyclerview, 그리고 완료되었습니다. 어댑터를 직접 작성해야하지만 Google에서 할 수있는 자습서가 많이 있습니다.

Here is a link 제가 RecyclerView에 대해 배우기 시작했을 때 제가 따라 왔다고 생각합니다. 때로는 이전 이었기 때문에 아직 정확한지 확신 할 수 없지만 시작으로 확인할 수 있습니다.

+0

활동에 코드를 작성하지 않고이를 수행 할 수 있습니까? 버튼 배열을 원합니다. 나중에 버튼을 사용하기 위해 onClick 메서드를 사용할 것입니다. 그래서 나는 버튼 배열을 원한다. 더 쉬운 방법이 있습니까? – DaFaack

+0

나는 정직하게 모른다. 하지만 RecyclerView를 사용하면 활동을 시작하기 위해 10 줄 미만의 코드를 작성할 수 있습니다. 대부분의 코드는 Adapter와 ViewHolder에있을 것입니다 (그리고 거기에 onClickListener를 추가 할 수 있습니다). –

관련 문제