我一直在玩這個,但我似乎無法讓它工作。我想將圖標儘可能移動到屏幕的最右側,但我找不到如何操作。通常情況下,這樣的東西可以工作,但由於某種原因,它不會。我認爲這與適配器顯示多行xml而非僅僅一行有關。如何對齊來自適配器的文本/圖像?
rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Loaded"
android:textSize="20px" >
</TextView>
<ImageView
android:id="@+id/icon"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:layout_gravity="right"
>
</ImageView>
</LinearLayout>
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
setListAdapter(adapter);
}
MySimpleArrayAdapter:
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
String s = values[position];
if (s.startsWith("iPhone")) {
imageView.setImageResource(R.drawable.yes);
} else {
imageView.setImageResource(R.drawable.no);
}
return rowView;
}
}
感謝您的幫助,您可以提供
編輯:woops,通過圖標我的意思圖像
使用的RelativeLayout代替的LinearLayout,並給予ImageView的屬性的android:alignParentRight =真 – roccocullo 2014-10-16 15:45:02
謝謝你的建議,我給它一個旋轉明天 – Ben 2014-10-16 20:34:40