对不起,我的小知识。我试图创建一个音板,所以我决定对其进行设计,例如:https://gyazo.com/2A08BCD731FB1CFEB07E72F75BC05E7E
一切都很好,我在华为P8 Lite和三星Galaxy S7上测试了它,一切都很好。但是,如果我在Bluestacks或Noxplayer上尝试它,则看起来像这样:https://gyazo.com/84D878061234C25AE872D4ED2491F2F4
这是我用来获取此信息的代码:
<?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:onClick="button1"
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:onClick="Button2"
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:onClick="Button3"
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:onClick="Button4"
android:text="Button"/>
//and 200 buttons more
</GridLayout>
</ScrollView>
我知道该代码有什么问题,例如" Gridlayout"。但是我不知道该如何进行编程。许多人试图解释它,并说我应该使用recyclerview或GridView,但是没有人将我的示例代码更改为正确的代码。每个人都给我发送了一个链接,其中包括300行解释。事情是我的英语很糟糕,所以我无法理解他们想解释我什么。如果有人可以通过一个例子向我解释这一点,我会很高兴。
我认为问题是这两行:
android:layout_width="100dp"
android:layout_height="100dp"
但是,如果我将两个设置为"填充",则按钮伸出了边缘。是否可以编写适合每个屏幕大小的代码,并且不在MainAttivity中编写任何代码?
如果有人可以向我展示正确的操作,我会很高兴:)
p.s不要火焰我我是一个15岁的德国男孩,这就是为什么我英语如此糟糕。
而不是gridlayout,我建议您使用recyclerview。使用RecyClerview,您可以使用GridlayOutManager。
这将帮助您维护屏幕兼容性,并将任何数量的值添加到列表中。无需通过XML布局设计来管理它。
我强烈建议您使用GridLayoutManager
使用RecyclerView
,但是如果您不想实现RecyclearView
,但是更改代码,则应尝试更改这两行代码:
android:layout_width="100dp"
android:layout_height="100dp"
如果将宽度和高度设置为100DP,则按钮将始终为100DP。因此,您应该将LinearLayout
用作父级布局,并为每个按钮使用android:layout_weight
,例如:
android:layout_width="0dp"
android:layout_weight = "1";
android:layout_height="100dp"
以这种方式,同一RAW上的按钮将拆分空间数量。