如何在那里的GridView包含按钮舒展在android系统网格视图中的行 [英] How to stretch rows of a grid view in android where gridview contains buttons

查看:146
本文介绍了如何在那里的GridView包含按钮舒展在android系统网格视图中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设计一个网格视图内的4x4按钮的计算器。现在,当我生成视图出现在网格下面的空白。我想网格完全填充父。就像 http://rechner-app.com/ 的例子。如何做到这一点。

activity_main.xml中

 <的TextView
    机器人:ID =@ + ID / txtStack
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginLeft =5SP
    机器人:layout_marginRight =5SP
    机器人:layout_marginTop =3SP
    机器人:重力=右
    机器人:TEXTSIZE =15sp/><的TextView
    机器人:ID =@ + ID / txtInput
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginLeft =5SP
    机器人:layout_marginRight =5SP
    机器人:重力=右
    机器人:TEXTSIZE =25sp/><的TextView
    机器人:ID =@ + ID / txtMemory
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginLeft =5SP
    机器人:layout_marginRight =5SP
    机器人:重力=左
    机器人:TEXTSIZE =15sp/><的LinearLayout
    机器人:ID =@ + ID / gridContainer
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:重力=fill_horizo​​ntal
    机器人:方向=垂直>
    < GridView控件
        机器人:ID =@ + ID / grdButtons
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:比重=中心
        机器人:horizo​​ntalSpacing =0.5dp
        机器人:为numColumns =4
        机器人:stretchMode =columnWidth时
        机器人:verticalSpacing =0.5dp/>
< / LinearLayout中>

适配器。

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    按钮BTN;
    如果(convertView == NULL){//如果它没有回收,初始化一些
                                //属性        BTN =新按钮(mContext);
        KeypadButton keypadButton = mButtons [位置]        btn.setBackgroundResource(R.drawable.keypad1);        //按钮mOnButtonClick集OnClickListener
        如果(keypadButton!= KeypadButton.DUMMY)
            btn.setOnClickListener(mOnButtonClick);
        其他
            btn.setClickable(假);
        //设置CalculatorButton枚举的按钮,这样的标签,我们
        //将利用我们的主要查看此信息,以确定哪些
        //做
        btn.setTag(keypadButton);
        查看rootView = LayoutInflater.from(mContext).inflate(R.layout.activity_main,父母,假);
        GridView控件mKeypadGridContainer =(GridView控件)rootView.findViewById(R.id.grdButtons);
        Log.d(TAG,mKeypadGridContainer.getHeight()+sadsd);
        Log.d(TAG,mKeypadGridContainer.getHeight()+:高度);
        //btn.setMinimumHeight(MainActivity.metricsHeight/4);
    }其他{
        BTN =(按钮)convertView;
    }    btn.setText(mButtons [位置] .getText());
    返回BTN;
}


解决方案

您将需要一个定制的复合控制。看到这里我的答案是:

<一个href=\"http://stackoverflow.com/questions/17023577/how-to-create-a-regular-resizable-grid-without-nested-weights/17025071#17025071\">How没有嵌套的权重,以创建一个常规,可调整大小的格?

更新

这似乎是一个很好的教程:

http://blog.tomgibara.com/post/ 1696552527 /工具 - 自己 - - Android的布局

您将有其修改为您的需求,您需要了解onMeasure和onLayout是如何工作的,但一旦你了解它,一切都落入地方,这很容易。

我有一个项目,我不得不建立这样的控制。如果我记得很清楚,我在使用MeasureSpec类的父的onMeasure()方法首先被称为儿童的措施(onMeasure方法)方法,然后我用getMeasuresWidth()和 - 高度用在父母的onLayout结果() ()方法,其中载有测量尺寸。这一切都在简单地说。

I am trying to design a calculator with buttons inside a grid view 4x4. Now when I generate the view there is white space below the grid. I want the grid to completely fill the parent. Just like http://rechner-app.com/ example. How to do this.

activity_main.xml

<TextView
    android:id="@+id/txtStack"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="5sp"
    android:layout_marginTop="3sp"
    android:gravity="right"
    android:textSize="15sp" />

<TextView
    android:id="@+id/txtInput"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="5sp"
    android:gravity="right"
    android:textSize="25sp" />

<TextView
    android:id="@+id/txtMemory"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="5sp"
    android:gravity="left"
    android:textSize="15sp" />

<LinearLayout
    android:id="@+id/gridContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="fill_horizontal"
    android:orientation="vertical" >
    <GridView
        android:id="@+id/grdButtons"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:horizontalSpacing="0.5dp"
        android:numColumns="4"
        android:stretchMode="columnWidth"
        android:verticalSpacing="0.5dp" />
</LinearLayout>

adapter.

public View getView(int position, View convertView, ViewGroup parent) {
    Button btn;
    if (convertView == null) { // if it's not recycled, initialize some
                                // attributes

        btn = new Button(mContext);
        KeypadButton keypadButton = mButtons[position];

        btn.setBackgroundResource(R.drawable.keypad1);

        // Set OnClickListener of the button to mOnButtonClick
        if (keypadButton != KeypadButton.DUMMY)
            btn.setOnClickListener(mOnButtonClick);
        else
            btn.setClickable(false);
        // Set CalculatorButton enumeration as tag of the button so that we
        // will use this information from our main view to identify what to
        // do
        btn.setTag(keypadButton);
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.activity_main, parent, false);
        GridView mKeypadGridContainer = (GridView) rootView.findViewById(R.id.grdButtons);
        Log.d(TAG, mKeypadGridContainer.getHeight() + "sadsd");
        Log.d(TAG, mKeypadGridContainer.getHeight() + " :height");
        //btn.setMinimumHeight(MainActivity.metricsHeight/4);
    } else {
        btn = (Button) convertView;
    }

    btn.setText(mButtons[position].getText());
    return btn;
}

解决方案

You will need a custom compound control. See my answer here:

How to create a regular, resizable grid without nested weights?

UPDATE

This seems to be a nice tutorial:

http://blog.tomgibara.com/post/1696552527/implement-your-own-android-layouts

You will have to modify it to your needs and you will need to understand how onMeasure and onLayout works, but once you understood it, everything is falling into place, and it's easy.

I had a project where I had to construct such a control. If I remember well, I called the childrens' onMeasure method with the measure() method first in the parent's onMeasure() method using the MeasureSpec class, then I used the results in the parent's onLayout() using the getMeasuresWidth() and -Height() methods, which contained the measured dimensions. That's all in a nutshell.

这篇关于如何在那里的GridView包含按钮舒展在android系统网格视图中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆