简体   繁体   English

GridView中的Android EditText大小

[英]Android EditText sizing in a GridView

I have two GridViews layed out side by side. 我有两个并排布局的GridView。

The first GridView contains variable objects, in this case some EditTexts, depending on the game mode. 第一个GridView包含变量对象,在这种情况下为一些EditText,具体取决于游戏模式。

The second GridView contains TextViews that label the rows. 第二个GridView包含标记行的TextView。

Because I use the first GridView for many different things, I have been creating the EditText objects programmatically. 因为我将第一个GridView用于许多不同的事情,所以我一直在以编程方式创建EditText对象。 However, I am having a hard time getting their size to match up with the EditText row markers. 但是,我很难使它们的大小与EditText行标记相匹配。

1) Should I be constructing my EditText objects in xml (and if so, where would I put it, and what properties would I have to set) ? 1)我应该在xml中构造我的EditText对象吗(如果是的话,我将其放置在哪里,必须设置哪些属性)?

2) If programmatically creating the EditTexts is ok, how can I resize them through code to line up with the row numbers? 2)如果以编程方式创建EditTexts没问题,如何通过代码调整它们的大小以与行号对齐?

从仿真器查看

After loading the GridView's objects from the layout recommended below, I am given a ClassCastException complaining about LayoutParams: 从下面推荐的布局中加载GridView的对象后,我得到一个ClassCastException抱怨LayoutParams:

public View getView(int position, View convertView, ViewGroup parent) {
            EditText textbox;
            View MyView = convertView;
            if (convertView == null) {

                LayoutInflater li = getLayoutInflater();
                MyView = li.inflate(R.layout.edittextfull, null);
                textbox = (EditText) MyView.findViewById(R.id.textbox);

            }
            else 
                textbox = (EditText) convertView;
            return textbox;
}

Here is your row 这是你的行

<LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content" >
                <EditText android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_gravity="center_vertical"
                         android:layout_weight="1"
                 />
                 <TextView  android:layout_width="wrap_content" 
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:text="row 1"
                 />              
    </LinearLayout>

to use xml is always better . 使用xml总是更好。 so avoid creating view pragmatically until its the only way . 所以避免务实地创建视图,直到它成为唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM