简体   繁体   中英

Android EditText sizing in a GridView

I have two GridViews layed out side by side.

The first GridView contains variable objects, in this case some EditTexts, depending on the game mode.

The second GridView contains TextViews that label the rows.

Because I use the first GridView for many different things, I have been creating the EditText objects programmatically. However, I am having a hard time getting their size to match up with the EditText row markers.

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) ?

2) If programmatically creating the EditTexts is ok, how can I resize them through code to line up with the row numbers?

从仿真器查看

After loading the GridView's objects from the layout recommended below, I am given a ClassCastException complaining about 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 . so avoid creating view pragmatically until its the only way .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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