Android Listview宽度问题

发布于 2024-12-14 11:15:33 字数 4251 浏览 4 评论 0原文

这是屏幕的主要布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/splashContent"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/splashscreenbg">
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".008"
        android:gravity="center"
        android:background="@drawable/mbstabtitle_bg"
        android:paddingTop="5dip" 
        android:paddingBottom="5dip">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center" >
            <TextView
                android:id="@+id/mbstabtitle_bg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textStyle="bold"
                android:text="Please Select Your Location"
                android:textColor="#FFFFFF">
            </TextView>     
        </LinearLayout>
    </TableRow>
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="top"
        android:layout_weight=".99">
        <ListView
            android:id="@+id/locationlist"
            android:background="#7B0326"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        </ListView>
    </TableRow>
</TableLayout>

这是用于填充列表视图的适配器。

class LocationAdapter extends BaseAdapter {
        private Activity activity;
        private String[][] data;
        private LayoutInflater inflater=null;

        public LocationAdapter(Activity a, String[][] d) {
            activity = a;
            data=d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return data.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            ViewHolder holder;
            if(convertView==null){
                vi = inflater.inflate(R.layout.locationitem, null);
                holder=new ViewHolder();
                holder.locationName=(TextView)vi.findViewById(R.id.locationName);

                vi.setTag(holder);
                if(position%2 == 0)
                    vi.setBackgroundColor(0xFF4D0418);
                else
                    vi.setBackgroundColor(0xFF7B0326);
            }
            else
                holder=(ViewHolder)vi.getTag();

            holder.locationName.setText(data[position][1]);          

            vi.setId(Integer.parseInt(data[position][0]));

            return vi;
        }

        class ViewHolder {
            TextView locationName;
        }
    }

这是项目的 xml (locationitem.xml)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center_vertical">
    <ImageView
        android:id="@+id/centerbullet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:src="@drawable/centerbullet"/>
    <TextView android:id="@+id/locationName" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:textStyle="bold"
        android:paddingLeft="10dip"
        android:text="hello hello egele efsdffgxnd"
        android:textSize="15dip" />
</LinearLayout>

现在我的问题是列表视图和其中的项目仅占用显示所需的最小宽度,而我希望它占用可用的总宽度。我的代码有什么问题?

Here is the main layout for a screen:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/splashContent"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/splashscreenbg">
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".008"
        android:gravity="center"
        android:background="@drawable/mbstabtitle_bg"
        android:paddingTop="5dip" 
        android:paddingBottom="5dip">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center" >
            <TextView
                android:id="@+id/mbstabtitle_bg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textStyle="bold"
                android:text="Please Select Your Location"
                android:textColor="#FFFFFF">
            </TextView>     
        </LinearLayout>
    </TableRow>
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="top"
        android:layout_weight=".99">
        <ListView
            android:id="@+id/locationlist"
            android:background="#7B0326"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        </ListView>
    </TableRow>
</TableLayout>

And here is the Adapter used to populate the listview.

class LocationAdapter extends BaseAdapter {
        private Activity activity;
        private String[][] data;
        private LayoutInflater inflater=null;

        public LocationAdapter(Activity a, String[][] d) {
            activity = a;
            data=d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return data.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            ViewHolder holder;
            if(convertView==null){
                vi = inflater.inflate(R.layout.locationitem, null);
                holder=new ViewHolder();
                holder.locationName=(TextView)vi.findViewById(R.id.locationName);

                vi.setTag(holder);
                if(position%2 == 0)
                    vi.setBackgroundColor(0xFF4D0418);
                else
                    vi.setBackgroundColor(0xFF7B0326);
            }
            else
                holder=(ViewHolder)vi.getTag();

            holder.locationName.setText(data[position][1]);          

            vi.setId(Integer.parseInt(data[position][0]));

            return vi;
        }

        class ViewHolder {
            TextView locationName;
        }
    }

And here is the xml for the items(locationitem.xml).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center_vertical">
    <ImageView
        android:id="@+id/centerbullet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:src="@drawable/centerbullet"/>
    <TextView android:id="@+id/locationName" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:textStyle="bold"
        android:paddingLeft="10dip"
        android:text="hello hello egele efsdffgxnd"
        android:textSize="15dip" />
</LinearLayout>

Now my problem is the the listview and the item in it taking just minimum width it required to display, whereas I want that it takes the total width available. What is the problem in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风柔一江水 2024-12-21 11:15:33

ListView 中的 android:layout_weight="1" 解决了这个问题。

android:layout_weight="1" in ListView solved the problem.

妖妓 2024-12-21 11:15:33

使用可以使用样式

style="?android:attr/spinnerItemStyle"

use can use style

style="?android:attr/spinnerItemStyle"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文