Android 具有2列的网格视图,无需在Activitys?中写入任何代码安卓工作室
对不起,我知之甚少。 我试图创建一个音板,所以我决定这样设计: 一切正常,我在我的华为P8 Lite和三星galaxy s7上进行了测试,一切正常。 但如果我在Bluestack或Noxplayer上尝试,它看起来是这样的: 以下是我用来获取此信息的代码:Android 具有2列的网格视图,无需在Activitys?中写入任何代码安卓工作室,android,xml,eclipse,button,layout,Android,Xml,Eclipse,Button,Layout,对不起,我知之甚少。 我试图创建一个音板,所以我决定这样设计: 一切正常,我在我的华为P8 Lite和三星galaxy s7上进行了测试,一切正常。 但如果我在Bluestack或Noxplayer上尝试,它看起来是这样的: 以下是我用来获取此信息的代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro
<?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>
但如果我将它们都设置为“填充父对象”,则按钮会伸出边缘。
是否可以编写适合每个屏幕大小的代码,而不在MainActivity中编写任何代码
如果有人能教我怎么做,我会非常高兴:)
别炒我,我是一个15岁的德国男孩,这就是我英语这么差的原因。我建议你不要用GridLayout,而是用RecyclerView。使用RecyclerView,您可以使用
这将帮助您维护屏幕兼容性,并将任意数量的值添加到列表中。无需通过xml布局设计对其进行管理。我强烈建议您将
RecyclerView
与GridLayoutManager
一起使用,但如果您不想实现RecyclerView
而更改代码,则应尝试更改这两行代码:
android:layout_width="100dp"
android:layout_height="100dp"
如果将宽度和高度设置为100dp,则按钮将始终为100dp。
因此,您应该使用LinearLayout
作为父布局,并对每个按钮使用android:layout\u weight
,如下所示:
android:layout_width="0dp"
android:layout_weight = "1";
android:layout_height="100dp"
这样,同一原始数据上的按钮将分割空间量。这些项目是动态的吗?(即来自任何web API或数据库)向我展示您的网格项视图xmlAhh为什么要为每个按钮创建布局只需使用带有GridLayout Manager的RecyclerView即可解决此问题。您只需为一个按钮创建布局并将其膨胀到GridLayout Manager请考虑您的屏幕截图。有些话很无礼。
android:layout_width="0dp"
android:layout_weight = "1";
android:layout_height="100dp"