如何布局按钮数组以适应任何屏幕大小

3
我已经为我的应用程序创建了一个按钮数组。现在我无法管理这些按钮数组的布局。因此,每当我添加图像或更改按钮的宽度时,它会超出设备的水平屏幕。是否有任何方法可以管理这些按钮数组,使它们适合任何屏幕尺寸。
以下是我的代码: 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);
        }
    }
}

}

插入图片之前的快照:

enter image description here

插入图像前的快照:

enter image description here


  1. 尝试减少按钮之间的间距。
  2. 尝试使用相对布局。
- Vinay
当我使用相对布局时,什么都没有显示,只有一个按钮。 - AndroidDev
您可以在相对布局中添加任意数量的按钮。也许您有其他问题。 - Vinay
3个回答

1

我知道这并不是直接回答你的问题,但我只是想帮助你。如果你正在构建一个日历应用程序,创建大量按钮真的不是正确的方法:

  1. 由于皮肤等原因,您将在不同的ROM上遇到问题。
  2. 您将无法完全控制布局(再次因为皮肤)
  3. 您将遇到间距问题
  4. 您将分配大量内存(许多Button对象等),这会导致您的应用程序变慢。

我建议的是实现自己的自定义View。最近我自己也开发了一个日历应用程序,并尝试使用GridView来显示月份,但那真的不太好用(虽然看起来很不错),所以我最终创建了自己的View,效果非常好。

我发现Android默认的开源Calendar应用程序非常有用。您将在其中找到源代码,包括月视图和日/周视图(带有小时刻度等)。


嗯,你说的没错,但我的任务是使用按钮数组创建自定义日历。请给我一些关于此事的建议。 - AndroidDev

1
你可以尝试使用TableLayout并设置可换行的列:
找到了一个很好的教程,里面有示例 -> Android TableLayout 教程

要使列可换行(如果表中的其他列占用太多空间并将某些列推出屏幕,则缩小它的宽度并换行其内容),请使用setColumnShrinkable标记它为可缩小。

听起来很有前途。

仅仅因为这个链接就值得点赞。它充满了对那些难以理解的TableLayout术语的精彩解释。 - SMBiggs

0

只需替换此代码。使用linear的weightsum属性和button的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);**
            }
        }
    }

}

enter image description here


谢谢,它工作得很好。还有一件事,它会自动调整每个屏幕大小吗?还是我们需要为此做单独的事情? - AndroidDev
嗨,Hitendra。我有一个问题。当我为每个按钮设置背景图像时,按钮就变得不活动了,这意味着我无法点击按钮。为什么会这样? - AndroidDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接