2012-07-18 90 views
1

我想添加一个图像到我的ListView,使它看起来更像一个按钮。我希望图像稍微小一点,大概是目前的60%。图像在列的右侧很好地排列。这里是什么,我现在有一个画面:Android - 如何对齐列表视图项目以良好的间距左右对齐?

enter image description here

,这里是我的列表视图中的xml:

<?xml version="1.0" encoding="utf-8"?> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:textSize="16sp"  
    android:layout_width="match_parent" 
    android:drawableRight="@drawable/arrow_button" 
    > 
</TextView> 

任何想法,我在做什么错误?

包含此TextView的ListView控件的定义如下:


一个说明,我创建并与我的列表的工作方式与ListAdapter,使用这样的代码:

Question q = new Question(); 
q.setQuestion("This is a test question and there are more than one"); 

questions.add(q); 

adapter = new ArrayAdapter<Question>(this, R.layout.questions_list, questions); 

setListAdapter(adapter); 

谢谢!

+3

Protip:Alt + PrtScr截取当前窗口的截图。或者因为您在Windows 7上,请使用“截取工具”。 – kcoppock 2012-07-18 03:46:25

回答

1

随着Frank Sposaro给出的评论和建议,您将能够正确定位您的视图。

你的下一个问题,我建议你做你自己的适配器与此类似:

private class CustomAdapter extends ArrayAdapter<Question> { 

     private LayoutInflater mInflater; 

     public CustomAdapter(Context context) { 
      super(context, R.layout.row); 
      mInflater = LayoutInflater.from(context); 
     } 

     public View getView(final int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 

      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.row, null); 

       holder = new ViewHolder(); 
       holder.text = (TextView) convertView.findViewById(R.id.mTextView); 
       holder.image = (ImageView) convertView.findViewById(R.id.mImage); 

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

      //Fill the views in your row 
      holder.text.setText(questions.get(position).getText()); 
      holder.image.setBackground... (questions.get(position).getImage())); 

      return convertView; 
     } 
    } 

    static class ViewHolder { 
     TextView text; 
     ImageView image; 
    } 

在你的onCreate:

ListView mListView = (ListView) findViewById(R.id.mListView); 
mListView.setAdapter(new CustomAdapter(getApplicationContext(), questions)); 

用于与适配器一个ListView另一个例子可以发现here

3

啊。你正在使用复合drawable来做正确的事情。不知道是否有更好的方法可能会扩大复合绘图中的间距,但我知道这会起作用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"> 

<TextView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:padding="10dp" 
    android:textSize="16sp" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" /> 

<View 
    android:layout_height="64dip" 
    android:layout_width="64dip" 
    android:background="@drawable/arrow_button" 
    android:layout_centerVertical="true" 
    android:layout_alignParentRight="true" /> 

</RelativeLayout> 

基本上只是指出使用对齐父母的权利和左派。您可能需要为它们添加一些边距或填充。还要确保垂直居中您的元素。

+0

谢谢,现在尝试。你会不会知道如何让图像看起来大约是它们当前尺寸的60%?他们似乎有点太大了。 – Genadinik 2012-07-18 03:30:36

+0

其实我在xml:Unbound前缀上出现RelativeLayout :( – Genadinik 2012-07-18 03:31:30

+0

这是因为xmlns:android =“http://schemas.android.com/apk/res/android”应该被移动了。它 – 2012-07-18 03:34:39