我为我的应用程序创建了一系列按钮。现在我无法管理这些按钮数组的布局。因此,每当我添加图像或更改按钮的宽度时,它就会离开设备的水平屏幕。因此,是否有任何方法来管理这些按钮阵列,使他们能够适应任何屏幕大小。这是我的代码:
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);
}
}
}}
插入图像前的快照:

插入图像前的快照:

发布于 2011-06-28 09:09:43
只需替换线性的code.use加权和属性和按钮的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);**
}
}
}
}

发布于 2011-06-28 08:50:04
我知道这不能直接回答你的问题,但我只是想帮你。如果您正在构建一个日历应用程序,那么创建大量的按钮实际上并不是一个好方法:
Button对象,等等)这会导致你的应用程序变慢。我推荐的是实现您自己的自定义View。最近,我自己开发了一个日历应用程序,并尝试了一个月使用GridView,但这确实不是很好(尽管看起来会这样),所以我最终创建了自己的View,它运行得非常完美。
我发现非常有用的是安卓的默认开源日历应用程序。您可以在其中找到一个月视图和一个日/周视图的源代码(有一个小时的比例,等等)。
发布于 2011-06-28 09:09:32
您可以尝试一个TableLayout并将列设置为可包装的:
找到了一个包含示例-> 安卓TableLayout教程的很好的教程
若要使列可包装(如果表中的其他列占用了太多的空间并将某些列从屏幕上推开),则使用setColumnShrinkable标记它的内容。
听起来很有希望
https://stackoverflow.com/questions/6503771
复制相似问题