【问题标题】:How to layout Array of buttons to fit in any screen size如何布局按钮阵列以适应任何屏幕尺寸
【发布时间】:2020-06-19 16:03:42
【问题描述】:

我为我的应用创建了一组按钮。现在我无法管理这些按钮数组的布局。因此,每当我添加图像或更改按钮的宽度时,它都会超出设备的水平屏幕。那么有什么方法可以管理这些按钮阵列,以便它们适合任何屏幕尺寸。 这是我的代码:

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

}

插入图片前的快照:

插入图片前的快照:

【问题讨论】:

  • 1.尝试减少按钮之间的间距。 2.尝试使用相对布局..
  • 当我使用相对布局时,只显示一个按钮..
  • 您可以在相对布局中添加任意数量的按钮。可能您还有其他问题

标签: android


【解决方案1】:

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

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

我建议实现您自己的自定义View。我最近自己开发了一个日历应用程序,并尝试在本月使用GridView,但效果确实不太好(尽管看起来会这样),所以我最终创建了自己的View,它成功了完美。

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

【讨论】:

  • yeh..你说的很中肯,但我的任务是使用按钮数组创建你自己的自定义日历..所以请给我一些建议
【解决方案2】:

您可以尝试TableLayout 并将列设置为可换行:

找到了一个很好的示例教程 -> Android TableLayout Tutorial

要使列可折叠(在表格中的其他列占用太多空间并将某些列从屏幕上推开的情况下减小其宽度并包裹其内容),请使用 setColumnShrinkable 将其标记为可收缩。

听起来很有希望

【讨论】:

  • +1 仅用于链接。它对那些该死的难以理解的 TableLayout 术语进行了很好的解释。
【解决方案3】:

只需替换此代码即可。使用线性的 weightsum 属性和按钮的 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);**
            }
        }
    }

}

【讨论】:

  • 感谢它的工作正常..还有一件事它会为每个屏幕尺寸自动调整..或者我们必须为此做单独的事情
  • 嗨hitendra ..一个问题..当我为每个按钮设置背景图像比变得不活动时,我无法点击按钮..为什么会发生这种情况..
猜你喜欢
  • 2021-06-21
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多