2011-06-28 54 views
3

我已经为我的应用程序创建了一组按钮..现在我无法管理这些按钮阵列的布局..由于每当我添加图像或chnage的宽度,它会从设备的水平屏幕熄灭..所以有任何管理这些按钮阵列的方法,以便它们可以适应任何屏幕尺寸..我写的代码是..对于XML如何布局按钮阵列以适合任何屏幕尺寸

代码:

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); 
     } 
    } 
} 
:对Java

<?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> 

代码

}

快照befor插入图片:

enter image description here

快照befor插入图片:

enter image description here

+0

1.尝试减少的按钮之间的间距。 2.尝试使用相对布局.. – Vinay

+0

当我使用相对布局没有任何东西只有一个按钮显示.. – AndroidDev

+0

您可以添加任何数量的按钮,你想在相对布局..可能你有其他问题 – Vinay

回答

0

只需更换线性和按钮的layout_weight这code.use weightsum属性。

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);** 
      } 
     } 
    } 

} 

enter image description here

+0

感谢它的工作正常..更多的事情,它会自动调整每个屏幕的大小..或我们必须做的分离的事情 – AndroidDev

+0

嗨hitendra..one问题..当我设置每个按钮的背景图像比变得不活动多数民众赞成在我无法点击按钮..为什么发生这.. – AndroidDev

1

我知道这并不直接回答你的问题,但我只是想帮助你。如果你正在构建一个日历应用程序,创建了大量的按键真的是没有要走的路:你不会有完全的控制

  1. 您将有因为皮肤的不同ROM的问题,等等。
  2. 在布局
  3. (因为皮肤再次)你会遇到问题间距
  4. 你会分配大量内存(很多Button对象等),这将导致你的应用程序要慢。

我推荐的是实现您自己的自定义View。我最近自己在一个日历应用程序上工作,并尝试在本月使用GridView,这确实不能很好地工作(尽管看起来像它),所以我最终创建了我自己的View,这个工作非常完美。

我发现非常有用的是Android的默认,开源Calendar应用程序。您可以在这里查找源代码,以查看月视图和日/周视图(带小时刻度等)。

+0

你..你说是至关重要的,但我的任务是使用按钮阵列来创建自己的cuctom日历..所以请给我一些关于这个 – AndroidDev

1

你可以尝试TableLayout并设置列可包装:

找到了一个不错的教程结合实例 - >Android TableLayout Tutorial

为了使一列可包装(以减少它的宽度和包裹它,如果其他内容表格中的列占用太多空间并将一些列从屏幕上移开),使用setColumnShrinkable将其标记为可缩小。

听起来前途

+0

+1的建议,仅仅用于链接。它充满了对这些该死的几乎不可理解的TableLayout条款的很好的解释。 –

相关问题