Android 如何创建1px边界网格
我想创建一个干净的网格。 我已经有了一些有用的东西,但是我有一个2px的边框。 这是我的密码: 在活动中:Android 如何创建1px边界网格,android,Android,我想创建一个干净的网格。 我已经有了一些有用的东西,但是我有一个2px的边框。 这是我的密码: 在活动中: setContentView(R.layout.activity_game); GridView gridview = (GridView) findViewById(R.id.gvGame); ButtonAdapter buttonAdapter = new ButtonAdapter(GameActivity.this, myList.getList()); gridview.set
setContentView(R.layout.activity_game);
GridView gridview = (GridView) findViewById(R.id.gvGame);
ButtonAdapter buttonAdapter = new ButtonAdapter(GameActivity.this, myList.getList());
gridview.setAdapter(buttonAdapter);
activity_game.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/gvGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@android:color/white"
android:gravity="center_vertical"
android:numColumns="3"
android:scrollbarStyle="outsideOverlay"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_button.xml(可绘制):
结果:
我怎样才能在任何地方都获得1px边界网格
注意:每个方块都必须是可点击的。我建议使用GridView的选项来实现均匀间隔的栅格
<GridView
...
android:horizontalSpacing="1px"
android:verticalSpacing="1px"
... />
这将在GridView的项目之间创建一个大小为1px的空间
如果希望间距为黑色,则必须将GridView(或其父对象)的背景设置为黑色
当然,现在您必须删除按钮背景的黑色边框。在您的
RelativeLayout顶部和左侧和右侧和底部的边框上加上1px
边框怎么样
这将产生您想要的效果,但如果最后一行中的单元格数较少,则无法确定您想要的结果是什么。好主意。我只需要在gridview上添加一点填充,以使边框可见。非常感谢。可用的解决方案。我想这会奏效,但这涉及到一点过度拉伸。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@android:color/black" />
</shape>
</item>
<item
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
</layer-list>
<GridView
...
android:horizontalSpacing="1px"
android:verticalSpacing="1px"
... />