2017-03-08 18 views
0

私はこのようなレイアウトを作成したいと考えています: https://gyazo.com/2a08bcd731fb1cfeb07e72f75bc05e7e これはどのデバイスに適していますか?どのように私はこのようなレイアウトをprogrammすることができますか?

画面は鉱山ですが、問題はすべての画面サイズに適合しないことです。それはこのようになりますいくつかのデバイスでは :ここ https://gyazo.com/84d878061234c25ae872d4ed2491f2f4

は、私はこのレイアウトをpragrammするために使用するコードです:

<?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> 

は、どのように私はすべての画面サイズに収まるこのようなレイアウトをプログラムの開発ができますか?重要 がScrollView

+0

カスタムアダプタで 'Gride view'や' RecyclerView'の 'GridAdapter'を検索する –

+0

アクティビティにコードを書くことなくこれを行うことはできますか?私はボタンの配置をしたいだけです – DaFaack

+0

あなたは何を意味するのか分かりませんが、 'ListView'でやったのと同じように' GridAdapter'を使うこともできますし、 'RecyclerView' AFAIKを使って達成することもできます! –

答えて

1

心に来た最初の考えはGridLayoutManager

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

でRecyclerViewを使用することです。これはあなたに2列の垂直ダウンリストが表示されます。心配しないでも、CardViewもチェックアウトしたいと思うかもしれませんが、時には一緒に使用したいと思うこともあります。

RecyclerView用のアダプタを作成したら、アクティビティにGridLayoutManagerとYourAdapterを設定することができます。これは実際にアクティビティに必要なものです。アダプタの初期化、レイアウトマネージャとアダプタの設定あなたのrecyclerviewの、そして終わった。アダプターを自分で書く必要がありますが、Googleでできるチュートリアルはたくさんあります。

Here is a link私がRecyclerViewについて学び始めたとき、私が以前に従ったと思います。それは時々だったので、それがまだ正しいかどうか分からないが、あなたは最初にそれをチェックすることができる。

+0

アクティビティにコードを記述しなくても可能ですか?私はそのボタンの配置が欲しいだけです。その後、ボタンを使用するためにonClickメソッドを使用します。だから私はボタンの配置だけをしたい。簡単な方法はありますか? – DaFaack

+0

私は正直に分かりません。しかし、RecyclerViewを使用すると、わずか10行未満のコードを記述してアクティビティを開始することができます。コードの大部分は、アダプタとViewHolderにあります(また、そこにあなたのonClickListenerを追加することもできます)。 –

関連する問題