【问题标题】:Layout Problems in ListviewListview 中的布局问题
【发布时间】:2013-04-08 03:01:23
【问题描述】:

我在设计方面有问题。我希望在 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>

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

【问题讨论】:

  • 不是错误,而是更多的问题。代码运行时。我只看到文字。当我从 xml 中删除文本条目时,我可以看到第一张图片,当我删除第一张图片等时...我需要一个足够大的行来查看所有内容
  • 你能详细说明你的问题吗?
  • 我已经详细说明了。基本上我如何在列表视图中的一行中获取两个图像和文本
  • 从这个链接获取想法:stackoverflow.com/a/12813906/1168654

标签: java android xml android-layout


【解决方案1】:

正如您提供的 XML,我可以看到 TextViewImageViewwidth 为“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>

【讨论】:

    【解决方案2】:

    这是我创建的答案!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多