2017-03-08 59 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