小编Bir*_*man的帖子

如何向 GridBagLayout 添加空单元格?

我想使用GridBagLayout来实现这种效果:

|-----------|
|  Button1  |
|-----------|
      |-----------|
      |  Button2  |
      |-----------|
Run Code Online (Sandbox Code Playgroud)

我最初只是放置了按钮,但 GridBagLayout 忽略了空单元格。现在,我尝试使用 Box.createHorizo​​ntalStrut(10) 作为间隔符,但 GridBagLayout 正在拉伸空间以给我一个 2x2 网格。我还尝试了 JLabels 和 JPanels 作为间隔物,但我仍然得到 2x2 网格。这是我的代码:

import java.awt.*;
import javax.swing.*;

public class GridBagLayoutDemo extends JFrame
{
    // class level references
    GridBagLayout layout;   
    JButton button1, button2;

    // constructor
    public GridBagLayoutDemo()
    {
        super("GridBagLayout Demo");

        layout = new GridBagLayout();
        setLayout( layout );

        // create components
        button1 = new JButton("Button 1");
        button2 = new JButton("Button 2");

        // components should be resized vertically AND horizontally …
Run Code Online (Sandbox Code Playgroud)

java swing constraints layout-manager gridbaglayout

2
推荐指数
1
解决办法
4507
查看次数

当手机旋转时,RecyclerView 不会从 2 列变为 3 列

当我将视图旋转为横向时,RecyclerView 不会从两列切换到三列。我尝试了这段代码:

mGLManager.setSpanCount(3);

mGLManager = (GridLayoutManager)mRecyclerView.getLayoutManager();
mGLManager.setSpanCount(2);
mRecyclerView.setLayoutManager(mGLManager);

mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 3) );
Run Code Online (Sandbox Code Playgroud)

我还尝试了这些行来刷新它:

mRecyclerView.refreshDrawableState();

mAdapter.notifyDataSetChanged();

mRecyclerView.invalidate();

mRecyclerView.requestLayout();

mRecyclerView.swapAdapter(mRecyclerView.getAdapter(), true);
mRecyclerView.scrollBy(0,0);
Run Code Online (Sandbox Code Playgroud)

这是我当前的代码。

public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // set the number of columns based on the orientation
        if( newConfig.orientation == Configuration.ORIENTATION_PORTRAIT ) {
            mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 2));
            mRecyclerView.invalidate();
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        } else if( newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ) {
            mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 3));
            mRecyclerView.invalidate();
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Toast 表明当我翻转方向时代码会运行。感谢您的帮助!

android gridlayoutmanager android-recyclerview

1
推荐指数
1
解决办法
1288
查看次数