我有一排按钮但是超出了屏幕的宽度。我试着让按钮变小但是没有效果。它有一排11个圆形按钮。我能做什么?最后我想制作一个11乘11的网格,有关如何做到这一点的更好的建议,然后是一个巨大的布局xml?
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:id="@+id/button1" android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
</TableRow>
</TableLayout>
</LinearLayout>
roundedbutton.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#9F2200"/>
<stroke android:width="2sp" android:color="#fff" />
<size
android:width="5dp"
android:height="5dp"/>
</shape>
添加了对答案的请求
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
Log.d(TAG, "Position before convertView " + position );
if (convertView == null)
{
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.item, null);
// set value into textview
Button btn= (Button) gridView.findViewById(R.id.grid_item_label);
if (position == 3)
{
Log.d(TAG, "Position inside convertView " + position );
btn.setText("G");
btn.setBackgroundColor(Color.parseColor("#2E3135"));
//btn.setBackgroundColor(R.drawable.goodguybutton);
}
else
{
btn.setText(buttonTextValues[position]);
}
}
else
{
gridView = (View) convertView;
}
return gridView;
}
答案 0 :(得分:1)
您可以将所有按钮包裹成如此对齐的线性布局。
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="O"
android:id="@+id/button1"
android:background="@drawable/roundedbutton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="O"
android:id="@+id/button1"
android:background="@drawable/roundedbutton"/>
</LinearLayout>
我发现你的问题实际上有两部分。你可以用两个嵌套for循环手动创建网格..
答案 1 :(得分:1)
gridView.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
item.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="O"
android:id="@+id/grid_item_label"
android:layout_column="1"
android:background = "@drawable/roundedbutton"/>
自定义按钮适配器类...您可以将其用作内部类
public class ButtonAdapter extends BaseAdapter {
private Context context;
private final String[] buttonTextValues;
public ButtonAdapter(Context context, String[] buttonTextValues) {
this.context = context;
this.buttonTextValues = textViewValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.item, null);
// set value into textview
Button btn= (Button) gridView
.findViewById(R.id.grid_item_label);
btn.setText(buttonTextValues[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return buttonTextValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
活性:
public class ButtonGridView extends Activity {
GridView gv;
String[] buttonText = {"1", "2"}; //and so on to 11*11
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridView);
gv =(GridView)findViewById(R.id.gridView1);
gv.setAdapter(new ButtonAdapter(this,buttonText));
}
}
另请注意,您可以根据需要传递任何信息以使用构造函数进行适配,并确定该按钮将具有任何颜色文本或任何您想要的...