GridView引力不会发生变化

时间:2017-09-29 18:43:15

标签: android xml android-layout gridview layoutparams

每当我点击按钮时,我都会尝试更改GridView的重力,但部署时应用程序崩溃了。我不确定为什么在XML布局中声明GridView时会发生这种情况。

  

尝试调用虚方法' android.view.ViewGroup $ LayoutParams android.widget.GridView.getLayoutParams()'在空对象引用上

sw600dp / activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:background="@android:color/transparent"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="top"
        android:onClick="moveup_click"
        />

    <GridView
        android:id="@+id/abslistview_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:numColumns="auto_fit"
        />

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:background="@android:color/transparent"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        android:onClick="movedown_click"
        />
</LinearLayout>

MainActivity.java     公共类MainActivity扩展了Activity {

    Activity mActivity;
    private Button btnUp;
    private Button btnDown;
    private GridView gridMain;

    private AbsListView absListView;
    private ArrayAdapter<MainItem> adapter;
    private ArrayList<MainItem> mainArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        absListView = findViewById(R.id.abslistview_main);
        settingData();

        adapter = new AbsListViewAdapter(this, R.layout.item_abslistview, mainArrayList);
        absListView.setAdapter(adapter);
    }

    private void settingData() {
        mainArrayList = new ArrayList<>();

        mainArrayList.add(new MainItem(R.drawable.ic_circle, getResources().getString(R.string.item_1)));
        mainArrayList.add(new MainItem(R.drawable.ic_star, getResources().getString(R.string.item_2)));
        mainArrayList.add(new MainItem(R.drawable.ic_square, getResources().getString(R.string.item_3)));
        mainArrayList.add(new MainItem(R.drawable.ic_triangle, getResources().getString(R.string.item_4)));
        mainArrayList.add(new MainItem(R.drawable.ic_rectangle, getResources().getString(R.string.item_5)));
        mainArrayList.add(new MainItem(R.drawable.ic_crescent, getResources().getString(R.string.item_6)));
    }

    public void moveup_click(View view) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) gridMain.getLayoutParams();
        gridMain = findViewById(R.id.abslistview_main);
        if (params.gravity == Gravity.BOTTOM) {
            gridMain.setGravity(Gravity.CENTER_VERTICAL);
            btnDown.setVisibility(View.VISIBLE);
        } else if (params.gravity == Gravity.CENTER_VERTICAL) {
            gridMain.setGravity(Gravity.TOP);
            btnUp.setVisibility(View.GONE);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您有触发moveup_click()功能的按钮。 在该函数中,您首先需要GridView的LayoutParams,然后初始化gridview本身。 这不是正确的顺序。

要解决此问题,您可以移动gridMain = findViewById(R.id.abslistview_main); oncreate功能或移动LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) gridMain.getLayoutParams();。   -

答案 1 :(得分:0)

您在创建方法

中也犯了一个错误
absListView = findViewById(R.id.abslistview_main); 

使用

删除此行
gridMain = findViewById(R.id.abslistview_main);