2

我有问题设计明智。我希望在 listView 的一行内水平显示两张图片。像这样:

  TextView
  ImageView
  ImageView

我想看看我是否可以只使用一个 xml,或者我是否必须考虑完全使用不同的视图。我通过 baseadapter 有 Listview 和自定义行 xml。详情见代码

    public class MyCustomBaseAdapter extends BaseAdapter {


        private Integer[] imgid = {
                R.drawable.up2,
                R.drawable.up3,
                R.drawable.up4,
                R.drawable.up5,
                R.drawable.up6,
                R.drawable.up7,
                R.drawable.up8,
                R.drawable.up9,
                R.drawable.down2,
                R.drawable.down3,
                R.drawable.down4,
                R.drawable.down5,
                R.drawable.down6,
                R.drawable.down7,
                R.drawable.down8,
                R.drawable.down9 };


        private static ArrayList<DisplayResults> searchArrayList;

        private LayoutInflater mInflater;

        public MyCustomBaseAdapter(Context context, List<DisplayResults> list) {
            searchArrayList = (ArrayList<DisplayResults>) list;
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return searchArrayList.size();
        }

        public Object getItem(int position) {
            return searchArrayList.get(position);
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_row, null);
                holder = new ViewHolder();
                //holder.txtName = (TextView)                         `     

                        convertView.findViewById(R.id.ServiceName);
                holder.imgPhoto = (ImageView) convertView.findViewById(R.id.Up);

                //holder.imgPhoto2= (ImageView) convertView.findViewById(R.id.Down);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            //holder.txtName.setText(searchArrayList.get(position).getName());      

      holder.imgPhoto.setImageResource(imgid[searchArrayList.get(position).getImageNumber()]);
            System.out.println(searchArrayList.get(position).getImageNumber());
            //holder.imgPhoto2.setImageResource(imgid[searchArrayList.get(position).getImageNumber2()]);

            return convertView;
        }

        static class ViewHolder {
            TextView txtName;
            ImageView imgPhoto;
            //ImageView imgPhoto2;
        }
    }



    public class satDataFrag extends Fragment {

    Data datas;

    ListView listView;
    LayoutInflater inflaterT;


    public satDataFrag(Data data)
    {
        this.datas=data;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {   
        View view = inflater.inflate(R.layout.sat, container, false);
        inflaterT = inflater;
        listView = (ListView) view.findViewById(R.id.listSat);

        //ImageView imgView;
        //imgView = (ImageView) view.findViewById(R.id.icon);

        List<DisplayResults> List =  new ArrayList<DisplayResults>();

        DisplayResults temp= new DisplayResults();
        for(int i=0; i<datas.features.size(); i++)
        {

        if(datas.features.get(i).attributes.ServiceTyp.equalsIgnoreCase("Satellite"))
            {
                temp.setName(datas.features.get(i).attributes.dbANAME);

    temp.setImageNumber(upKey(datas.features.get(i).attributes.maxADUP));
    temp.setImageNumber2(downKey(datas.features.get(i).attributes.maxADDOWN));
                List.add(temp);
            }
        }

        //ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(getActivity(),
            //    R.layout.list_row, List);

        listView.setAdapter(new MyCustomBaseAdapter(getActivity(), List)); 
        return view;    

        }

    public Integer upKey (String max)
    {

        if(max.equalsIgnoreCase("2"))
        {
            return 0;
        }
        else if(max.equalsIgnoreCase("3"))
        {
            return  1;
        }
        else if(max.equalsIgnoreCase("4"))
        {
            return 2;
        }
        else if(max.equalsIgnoreCase("5"))
        {
            return  3;
        }
        else if(max.equalsIgnoreCase("6"))
        {
            return  4;
        }
        else if(max.equalsIgnoreCase("7"))
        {
            return  5;
        }
        else if(max.equalsIgnoreCase("8"))
        {
            return  6;
        }
        else if(max.equalsIgnoreCase("9"))
        {
            return  7;
        }
        else 
        {
            return 7;
        }

    }

    public Integer downKey (String max)
    {

        if(max.equalsIgnoreCase("2"))
        {
            return 8;
        }
        else if(max.equalsIgnoreCase("3"))
        {
            return  9;
        }
        else if(max.equalsIgnoreCase("4"))
        {
            return 10;
        }
        else if(max.equalsIgnoreCase("5"))
        {
            return  11;
        }
        else if(max.equalsIgnoreCase("6"))
        {
            return  12;
        }
        else if(max.equalsIgnoreCase("7"))
        {
            return  13;
        }
        else if(max.equalsIgnoreCase("8"))
        {
            return  14;
        }
        else if(max.equalsIgnoreCase("9"))
        {
            return  15;
        }
        else 
        {
            return 7;
        }
    }

}

    custom_row.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">    

   <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/ServiceName"
    android:text=""/>

    <ImageView
        android:id="@+id/Up"
        android:layout_gravity="left"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        />

    <ImageView 
        android:id="@+id/Down"
        android:layout_gravity="left"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    </LinearLayout>

所以我的问题是:有没有办法在列表视图内的一行中显示两个图像和一个文本?

4

2 回答 2

0

正如您提供的 XML,我可以看到TextViewImageView宽度为“match_parent”。因此,删除该代码并替换为以下代码。

我正在向 TextView 提供“wrap_content”。这将为 TextView 在渲染后提供所需的空间。并且 ImageView 提供了“100dp”的宽度,所以它们不会超过它,因为可能是图像很大,如果我们将它们设置为“wrap_content”,它们会占据整个宽度和高度。

<?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">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/ServiceName"
        android:text=""/>

    <ImageView
        android:id="@+id/Up"
        android:layout_gravity="left"
        android:layout_width="100dp"
        android:layout_height="wrap_content"/>

    <ImageView 
        android:id="@+id/Down"
        android:layout_gravity="left"
        android:layout_width="100dp"
        android:layout_height="wrap_content" />

</LinearLayout>

还有另一个选项可用,“重量”。

您可以为视图的内部线性布局提供权重。

<?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">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:id="@+id/ServiceName"
        android:text=""/>

    <ImageView
        android:id="@+id/Up"
        android:layout_gravity="left"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"/>

    <ImageView 
        android:id="@+id/Down"
        android:layout_gravity="left"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

</LinearLayout>
于 2013-04-08T05:56:43.587 回答
0

这是我创建的答案!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

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

        <TextView
            android:id="@+id/ServiceName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="left" />

    </LinearLayout>

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

        <ImageView
            android:id="@+id/Down"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="left">

        <ImageView
            android:id="@+id/Up"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>
于 2013-04-15T02:58:03.630 回答