2017-03-08 58 views
0

我用GridLayout製作了一個Soundboard。這是它應該是什麼樣子,以及它在我的華爲P8 Lite上的外觀:https://gyazo.com/2a08bcd731fb1cfeb07e72f75bc05e7e如何使用GridLayout?

這就是它在我的朋友的Bluestacks,Noxplayer和Device上的外觀。

https://gyazo.com/84d878061234c25ae872d4ed2491f2f4

這裏IST的XML代碼:(這與僅2行按鈕的一個例子,normaly有100行)

<?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:text="Button"/> 

      <Button 
       android:id="@+id/Button2" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_columnWeight="1" 
       android:layout_rowWeight="1" 
       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="1" 
       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="0" 
       android:layout_row="1" 
       android:text="Button"/> 

    </GridLayout> 

</ScrollView> 

我想問題各自的這兩條線按鈕

android:layout_width="100dp" 
    android:layout_height="100dp" 

如果我將它們都設置爲「fill_parent」,則按鈕會伸出屏幕。

我該如何解決這個問題? 請讓我看看正確的代碼,因爲我的英語很糟糕,而且我是Android Studio的新手。

回答

0

不,你沒有正確使用GridView。
GridView是一個AdapterView,這意味着您不應該使用XML定義它的子項,而是將它傳遞給Java代碼中的一個Adapter,它將創建並設置子項,使其可見。

有一個完整的Android開發者頁面,可以解釋GridView here

0

有一個把寬度設置爲屏幕大小一半的技巧。

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android: id="@+id/adjustment_linlayout" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:orientation="horizontal"> 
       <View 
        android:id="@+id/view1" 
        android: layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1"/> 
        <View 
        android:id="@+id/view2" 
        android: layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1"/> 
    </LinearLayout> 

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

     <Button 
      android:id="@+id/button1" 
      android:layout_width="@id/view1" 
      android:layout_height="100dp" 
      . 
      . 
      . 
</GridLayout> 

您將不得不設置layout_height一些dip值,否則Button將覆蓋整個網格或根本不可見。

但是最好的方法是使用PercentRelativeLayout https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html