java gridlayout 如何实现30行2列如图示的排列?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java gridlayout 如何实现30行2列如图示的排列?相关的知识,希望对你有一定的参考价值。
如图,我的要求其实很简单: 采用java gridlayout 30行2列,左边列是JLabel,右边是编辑框 JTextfield。我要求左边的JLabel 为输入框名称,宽度小一些,其显示的文本靠近输入框,右边的JTExtfield 宽度大一些,而实际程序总是给我显示左右两边一样宽,对半的,Jlabel显示的文本离输入框很遥远,看起来很不舒服,为何这么简单的要求,能不能实现呢?如何实现?多谢了!
参考技术A GridLayout是这样的,都是平均分配的。根据楼主的需求,我提供两种思路:其一:使用JSplitPane分割,以下代码:
private void init
//左侧布局
JPanel left = new JPanel() ;
left.setLayout(new GridLayout(30,1,1,1));
for(int i=0;i<30;i++)
//添加JLabel
//右侧布局
JPanel right = new JPanel() ;
right.setLayout(new GridLayout(30,1,1,1));
for(int i=0;i<30;i++)
//添加JTextField
//整体布局
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,right);//使用JSplitPane
jsp.setDividerLocation(100);
this.add(jsp) ;
this.setLocation(100, 100);
this.setSize(400, 800);
this.setVisible(true);
注意:jsp.setDividerLocation(100);方法是设置分隔栏的位置;其中数字要配合this.setSize(400, 800);使用,长400,分隔栏100,也就是大约左侧占四分之一,右侧占四分之三。这种方法比较灵活。
其二:使用BorderLayout布局器,代码就不写了,太麻烦
就是把左边的JLabel布局好以后放在WEST中,右侧的JTextField布局好后放在CENTER中。 参考技术B 貌似这个组件没办法设置不同宽带的列!你可以用 JTable这个组件!
以编程方式将视图添加到 Xamarin.Android C# 中的 GridLayout
【中文标题】以编程方式将视图添加到 Xamarin.Android C# 中的 GridLayout【英文标题】:Programmatically add view to a GridLayout in Xamarin.Android C# 【发布时间】:2021-12-02 06:06:16 【问题描述】:如何在 Xamarin.Android 中使用 C# 以编程方式创建视图并将其添加到 GridLayout,同时指定列/行跨度?
【问题讨论】:
【参考方案1】:您可以在指定列和行跨度时执行此操作,但是在 C# 中执行此操作的过程与 Java 不同。创建 GridLayout:
//Create the Grid Layout with 2 rows and 2 columns.
GridLayout sampleGridLayout = new GridLayout(mainActivity);
sampleGridLayout.RowCount = 2;
sampleGridLayout.ColumnCount = 2;
//Assuming you're adding the GridLayout to a LinearLayout.
//Replace the layout parameters as needed.
var gridLayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
sampleGridLayout.LayoutParameters = gridLayoutParameters;
//Create a Text View.
TextView sampleTextView1 = new TextView(mainActivity);
sampleTextView1.Text = "Sample Text 1";
//The GridLayout.LayoutParams constructor being used is this:
//GridLayout.LayoutParams(GridLayout.Spec? rowSpec, GridLayout.Spec? columnSpec)
//When invoking "Spec", the first integer is the 0 based column/row index(what row/column the view is in), the 2nd integer is the column/row span.
//So this is putting it in the first row, first column, only spanning a single cell.
var sampleTextView1Parameters = new GridLayout.LayoutParams(GridLayout.InvokeSpec(0, 1), GridLayout.InvokeSpec(0, 1));
sampleTextView1.LayoutParameters = sampleTextView1Parameters;
//Create a second Text View.
TextView sampleTextView2 = new TextView(mainActivity);
sampleTextView2.Text = "Sample Text 2";
//This is putting it in the first row, second column, only spanning a single cell.
var sampleTextView2Parameters = new GridLayout.LayoutParams(GridLayout.InvokeSpec(0, 1), GridLayout.InvokeSpec(1, 1));
sampleTextView2.LayoutParameters = sampleTextView2Parameters;
//Create a third Text View.
TextView sampleTextView3 = new TextView(mainActivity);
sampleTextView3.Text = "Sample Text 3";
//This is putting it in the second row, first column, but it spans both columns.
var sampleTextView3Parameters = new GridLayout.LayoutParams(GridLayout.InvokeSpec(1, 1), GridLayout.InvokeSpec(0, 2));
sampleTextView3.LayoutParameters = sampleTextView3Parameters;
sampleGridLayout.AddView(sampleTextView1);
sampleGridLayout.AddView(sampleTextView2);
sampleGridLayout.AddView(sampleTextView3);
这将创建一个 GridLayout,其中第一行有两个并排的 TextView,而第二行是跨越两列的单个 TextView。
【讨论】:
以上是关于java gridlayout 如何实现30行2列如图示的排列?的主要内容,如果未能解决你的问题,请参考以下文章