图形布局和设备中的UI不一样[Android]

时间:2012-05-14 09:07:01

标签: android xml listview layout

图形布局中的ListView适合适当的高度,但是当我导入到Android设备中时,ListView会在一个小字段中缩小。我希望ListView在xml文件和真实设备屏幕中的图形布局中是相同的

 <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        android:layout_margin="10sp"
        android:id="@+id/header"
         >
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/id"
        android:layout_weight="1.25"
        android:gravity="center"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/amount"
        android:layout_weight="1"
        android:gravity="center"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/alert"
        android:layout_weight="1"
        android:gravity="center"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/detail"
        android:layout_weight="1"
        android:gravity="center"/>
    </LinearLayout>

<ListView 
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_margin="10sp"></ListView>


<RelativeLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2.5">
<Button 
     android:id="@+id/addQueue"
     android:layout_width="80dp"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"
     android:layout_margin="10dp"
     android:text="@string/addQueue"/>
 <Button 
     android:id="@+id/submit"
     android:layout_width="80dp"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
     android:layout_margin="10dp"
     android:text="@string/submit"/>
  <Button
     android:id="@+id/reset"
     android:layout_width="80dp"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/addQueue"
     android:layout_centerHorizontal="true"
     android:layout_margin="10dp"
     android:text="@string/resetQueue" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我试图修改你的布局。试试这可能会对你有所帮助..只需创建一个新的xml布局文件并检查它是否适合你。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/addQueue"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/submit"
        android:text="addQueue" />

    <Button
        android:id="@+id/submit"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="submit" />

    <Button
        android:id="@+id/reset"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="resetQueue" />

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.25"
            android:gravity="center"
            android:text="text1" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="text2" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="text3" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="text4" />

    </LinearLayout>


    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/addQueue"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/header" >

    </ListView>

</RelativeLayout>

我的自定义适配器类在这里。 :

public class my_custom_adapter extends ArrayAdapter<String> {
    private Context context                     = null;
    String[]  elements                 = null;
    private ArrayList<Boolean> itemChecked      = null;

    public my_custom_adapter(Context context, int type, String[]  elements2)
    {
        super(context, type, elements2);
        this.elements =  elements2;
        this.context = context;
        itemChecked = new ArrayList<Boolean>();
        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals("master_check_change")) {
                    boolean check_value = intent.getBooleanExtra("check_value", false);
                    set_checked(check_value);
                    notifyDataSetChanged();
                }
            }
        };
        context.registerReceiver(receiver, new IntentFilter("master_check_change"));
        set_checked(false);
    }

    // AS EVERY TIME LISTVIEW INFLATE YOUR VIEWS WHEN YOU MOVE THEM SO YOU NEED TO SAVE ALL OF YOUR CHECKBOX STATES IN SOME ARRAYLIST OTHERWISE IT WILL SET ANY DEFAULT VALUE.
    private void set_checked(boolean is_checked)
    {
        for (int i=0; i < elements.length; i++) {
            itemChecked.add(i, is_checked);
        }
    }

    //THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
    static class ViewHolder
    {
        public TextView tv;
        public CheckBox cb;
        public ImageView iv;
    }

    @Override
    public View getView (final int position, View convertView, ViewGroup parent)
    {
        View rowView = convertView;
        ViewHolder holder = null;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                                               Context.LAYOUT_INFLATER_SERVICE);
            // HERE I AM INFLATING LISTVIEW LAYOUT.
            rowView = inflater.inflate(R.layout.inflated_layout, null, false);
            holder = new ViewHolder();
            holder.cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
            holder.tv = (TextView) rowView.findViewById(R.id.textView1);
            holder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
            rowView.setTag(holder);

        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        if (holder != null) {
            holder.tv.setText(elements[position]);

            holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                                             boolean isChecked) {
                    itemChecked.set(position, isChecked);
                }
            });

            if(position < itemChecked.size()) {
                holder.cb.setChecked(itemChecked.get(position));
            }
        }
        return rowView;
    }
}

我的活动类在这里,我创建自定义适配器对象,

public class Test_stflowActivity extends Activity {
    public CheckBox master_cb = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temp_layout);
        String[] elements = new String[10];
        for (int i = 0; i < 10; i++) {
            elements[i] = "elements " + i;
        }

        CheckBox master_cb = new CheckBox(getApplicationContext());
        master_cb.setText("Check All");
        //HERE IS THE LIST VIEW WHICH I HAVE CREATED IN MY XML FILE.
        ListView lv = (ListView) findViewById(R.id.listView1);
        //HERE I AM CREATING CUSTOM ADAPTER OBJECT.
        my_custom_adapter adapter = new my_custom_adapter(this, android.R.layout.simple_list_item_1, elements);
        lv.addHeaderView(master_cb);
        lv.setAdapter(adapter);
        master_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Intent my_intent = new Intent("master_check_change");
                my_intent.putExtra("check_value", isChecked);
                sendBroadcast(my_intent);
            }
        });
    }
}