我正在尝试创建一个按钮网格。为了显示GridLayout
的分隔符,我将GridLayout
的背景设置为黑色&然后我尝试放置我的按钮。我是在网格中得到分隔线,但由于某种原因,我无法控制它们的厚度。
我尝试将verticalSpacing
和horizontalSpacing
设置为低至0.1,但仍然没有用。它不会减少,我最终获得以下
以下是我的 content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="accordiontry.juspay.accordiontry.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">
<GridView
android:id="@+id/gridView"
android:padding="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:stretchMode="columnWidth"
android:gravity="fill_horizontal"
android:numColumns="2"
android:verticalSpacing=".05dp"
android:horizontalSpacing="0.1dp">
</GridView>
</LinearLayout>
以下是按钮的布局, cell_layout.xml
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button"
xmlns:android="http://schemas.android.com/apk/res/android" />
以下是自定义网格适配器 GridAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
/**
* Created by stpl on 27/7/16.
*/
public class GridAdapter extends BaseAdapter {
private Context context;
private String[] names;
public GridAdapter(Context context, String[] names)
{
this.context = context;
this.names = names;
}
@Override
public int getCount() {
return names.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View cell;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
cell = inflater.inflate(R.layout.cell_layout,null);
Button btn = (Button) cell.findViewById(R.id.button);
btn.setText("BUTTON "+ names[i]);
return cell;
}
}
最后,以下是我的 MainActivity.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private final int NumOfCol = 2;
private final int NumOfRows = 2;
private String[] names = {"BUTTON1", "BUTTON2", "BUTTON3", "BUTTON4"};
private GridAdapter gridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void init()
{
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridAdapter(this, names);
gridView.setAdapter(gridAdapter);
}
}
我不知道我哪里出错了。请帮忙。
感谢您的时间!
答案 0 :(得分:0)
按钮通常有自己的内置边距,来自用作按钮背景的drawable。
解决此问题的一种方法是为保证金指定负值:
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="-2dp" />
更好的方法是为没有定义任何边距的按钮创建一个drawable,并将其用于按钮背景。例如,这里是一个可从appcompat库中绘制的按钮背景(这个是Lollipop之前的版本并且没有显示波纹):
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/abc_button_inset_horizontal_material"
android:insetTop="@dimen/abc_button_inset_vertical_material"
android:insetRight="@dimen/abc_button_inset_horizontal_material"
android:insetBottom="@dimen/abc_button_inset_vertical_material">
<shape android:shape="rectangle">
<corners android:radius="@dimen/abc_control_corner_material" />
<solid android:color="@android:color/white" />
<padding android:left="@dimen/abc_button_padding_horizontal_material"
android:top="@dimen/abc_button_padding_vertical_material"
android:right="@dimen/abc_button_padding_horizontal_material"
android:bottom="@dimen/abc_button_padding_vertical_material" />
</shape>
</inset>
那些内容是让你悲伤的部分。只需为没有插入物的按钮背景定义另一个drawable。