2017-03-08 47 views
0

我想創建一個看起來像這樣的佈局: https://gyazo.com/2a08bcd731fb1cfeb07e72f75bc05e7e 我應該使用什麼來適應每個設備?如何編程這樣的佈局?

屏幕是我的,但問題是它不適合每個屏幕尺寸。 在某些設備上,它看起來是這樣的: 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> 

我怎麼能PROGRAMM這樣的佈局,適合於每一個屏幕大小? 重要的是浮現在腦海的滾動型

+0

使用自定義適配器爲'Gride view'或'RecyclerView'搜索'GridAdapter' –

+0

是否可以在沒有在活動中編寫任何代碼的情況下執行此操作?我只想要按鈕的排列 – DaFaack

+0

我不確定你的意思,但它可以像使用'ListView'一樣使用'GridAdapter',也可以使用'RecyclerView' AFAIK來實現! –

回答

1

首先想到的是用RecyclerView與GridLayoutManager

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

這會給你2列垂直下降的列表。如果您不介意的話,您可能還想查看CardView,但有時我喜歡將它們一起使用。

一旦您爲RecyclerView編寫了適配器,您可以在您的活動中設置GridLayoutManager和YourAdapter,這幾乎是您在活動中實際需要的唯一東西:初始化適配器,設置佈局管理器和適配器的回收者視圖,並完成。您需要自己編寫適配器,但有許多教程可供Google使用。

Here is a link我想我以前跟着我開始學習RecyclerView。它有時候是以前我不確定它是否仍然正確,但你可以把它作爲一個開始。

+0

如果不在活動中編寫任何代碼,是否可以這樣做?我只想要按鈕的排列。之後,我將使用onClick方法來使用按鈕。所以我只想要按鈕的排列。有更簡單的方法嗎? – DaFaack

+0

我真的不知道。但是使用RecyclerView,您可以編寫儘可能少於10行的代碼來在活動中啓動它們。大部分代碼將在適配器和ViewHolder中(您也可以在其中添加onClickListener)。 –