我已经为我的应用程序创建了一个按钮数组..现在我无法管理这些按钮数组的布局......因为每当我添加图像或chonage宽度的按钮时,它就会消失设备的水平屏幕..所以有任何方式来管理这些按钮阵列,以便它们可以适合任何屏幕尺寸..我写的代码是..
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/liVLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:id="@+id/liVLayout1"
android:orientation="vertical"
android:layout_below="@+id/liVLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="All Contacts"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:background="#808080">
</TextView>
</LinearLayout>
</RelativeLayout>
java代码:
public class CalenderForm extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createCalender();
}
public void createCalender()
{
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams
(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[6][7];
int count=43;
for (int i = 0; i<6; i++)
{
if(count%7==1)
{
rowLayout = new LinearLayout(this);
layoutVertical.addView(rowLayout,p);
count=count-7;
}
for(int j=0;j<7;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setBackgroundResource(R.drawable.icon);
rowLayout.addView(buttons[i][j], p);
}
}
}
}
快照插入图片:
快照插入图片:
答案 0 :(得分:1)
我知道这并没有直接回答你的问题,但我只是想帮助你。如果您正在构建日历应用程序,那么创建大量按钮确实不是可行的方法:
答案 1 :(得分:1)
您可以尝试TableLayout
并设置可包装的列:
找到一个包含示例的精彩教程 - &gt; Android TableLayout Tutorial
要使列可以包装(如果表中的其他列占用太多空间并将某些列从屏幕上移开,则减小其宽度并包装其内容),使用setColumnShrinkable将其标记为可收缩。
听起来很有希望
答案 2 :(得分:0)
只需替换此代码。使用linear的linearum属性和按钮的layout_weight。
package com.android.manager;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createCalender();
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[6][7];
int count=43;
**LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT,1);**
for (int i = 0; i<6; i++)
{
if(count%7==1)
{
rowLayout = new LinearLayout(this);
**rowLayout.setWeightSum(7);**
**layoutVertical.addView(rowLayout,param);**
count=count-7;
}
for(int j=0;j<7;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setBackgroundResource(R.drawable.icon);
**rowLayout.addView(buttons[i][j],param);**
}
}
}
}