Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从适配器对齐文本/图像?_Android_Layout_Adapter - Fatal编程技术网

Android 如何从适配器对齐文本/图像?

Android 如何从适配器对齐文本/图像?,android,layout,adapter,Android,Layout,Adapter,我一直在玩这个,但我似乎无法使它工作。我想将图标移动到屏幕最右边,但我找不到如何移动。通常,像这样的东西会起作用,但由于某种原因,它不起作用。我认为这和适配器显示多行xml有关,而不仅仅是一行 rowlayout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_wid

我一直在玩这个,但我似乎无法使它工作。我想将图标移动到屏幕最右边,但我找不到如何移动。通常,像这样的东西会起作用,但由于某种原因,它不起作用。我认为这和适配器显示多行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>
MySimpleArrayaAdapter:

 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;
    }
 }
公共类mysimplearlayadapter扩展了ArrayAdapter{
私人最终语境;
私有最终字符串[]值;
公共MySimpleArrayAdapter(上下文上下文,字符串[]值){
super(上下文、R.layout.rowlayout、值);
this.context=上下文;
这个值=值;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.rowlayout,父级,false);
TextView TextView=(TextView)rowView.findViewById(R.id.label);
ImageView ImageView=(ImageView)rowView.findViewById(R.id.icon);
setText(值[位置]);
字符串s=值[位置];
如果(s.startsWith(“iPhone”)){
setImageResource(R.drawable.yes);
}否则{
imageView.setImageResource(R.drawable.no);
}
返回行视图;
}
}
谢谢你的帮助


编辑:woops,我所说的图标是指图像

这是您想要实现的目标吗

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

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label"
    android:textSize="20sp" />

<ImageView
    android:id="@+id/icon"
    android:layout_width="22dp"
    android:layout_height="22dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_toLeftOf="@id/icon"
    android:layout_toRightOf="@id/label"
    android:text="Loaded"
    android:textSize="20sp" />
</RelativeLayout>

请注意,您不应将px用作任何对象的维度。相反,对于宽度、高度、边距或填充物等大小,请使用dp,对于文本大小,请始终使用sp

另外,为了获得更好的性能,请检查ListView适配器的ViewHolder模式。这是指适配器的getView()方法中的代码


这有用吗?

您必须使用RelativeLayout而不是LinearLayout

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" >
<TextView
    android:id="@+id/label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="20px"/>

<TextView
android:id="@+id/load"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
          android:layout_below="@id/label"
    android:text="Loaded"
    android:textSize="20px" />

<ImageView
    android:id="@+id/icon"
    android:layout_width="22px"
    android:layout_height="22px"
          android:layout_below="@id/label"
    android:layout_marginTop="4px"
    android:layout_alignParentRight="true" 
     />
    </RelativeLayout>


使用RelativeLayout而不是LinearLayout,并给ImageView属性android:alignParentRight=True谢谢你的建议,我明天会给它一个旋转谢谢你的建议,我明天会查看我的荣幸。祝你好运谢谢你的建议,我明天试试
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" >
<TextView
    android:id="@+id/label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="20px"/>

<TextView
android:id="@+id/load"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
          android:layout_below="@id/label"
    android:text="Loaded"
    android:textSize="20px" />

<ImageView
    android:id="@+id/icon"
    android:layout_width="22px"
    android:layout_height="22px"
          android:layout_below="@id/label"
    android:layout_marginTop="4px"
    android:layout_alignParentRight="true" 
     />
    </RelativeLayout>