行ListView Android之间的距离

时间:2016-07-06 08:43:45

标签: java android xml listview

我想创建一个ListView,其中包含每行中的ImageView和TextView。我能够做到这一点,但我有一个问题,改变行之间的距离。我想我不知何故改变了它,但我不知道如何。

ActorAdapter.java:

ArrayList<Actors> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;

public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
    super(context, resource, objects);
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    actorList = objects;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;
    if (v == null) {
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.image= (ImageView) v.findViewById(R.id.miniimage);
        holder.name = (TextView) v.findViewById(R.id.name);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("megnyomtam","a fost");
            }
        });
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("megnyomtam","a fost");
            }
        });
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    holder.image.setImageResource(R.drawable.ic_launcher);
    new DownloadImageTask(holder.image).execute(actorList.get(position).getImageurl());
    holder.name.setText(actorList.get(position).getTitle());
    return v;

}

static class ViewHolder {
    public ImageView image;
    public TextView name;

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }

}

ActorFragment.java

ArrayList<Actors> actorsList;
ActorAdapter adapter;
ListView listview;

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.activity_actor,parent, false);
    actorsList = new ArrayList<Actors>();
    new JSONAsyncTask().execute("http://demo.breona.hu:8080/mobil/mobilService?method=getAllData&appid=ESK");

    listview = (ListView)v.findViewById(R.id.list25);
    adapter = new ActorAdapter(getActivity().getApplicationContext(), R.layout.listview_row_layout, actorsList);

    listview.setAdapter(adapter);

    return v;
}

问题可能隐藏在xmls中,所以我也复制了它。

listview_row_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:background="@drawable/background">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/rellayout">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:id="@+id/miniimage"
        android:src="@drawable/ic_launcher"
        android:onClick="onClick"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/miniimage"
        android:text="Cherry"
        android:textColor="#000000"
        android:onClick="onClick"/>

</LinearLayout>

activity_actor.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"    android:background="#F1F1F1"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/list25"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:listitem="@layout/listview_row_layout">

</ListView>

以下是我想看到的图片: https://www.bignerdranch.com/blog/customizing-android-listview-rows-subclassing /

2 个答案:

答案 0 :(得分:1)

为了更改行之间的距离,您可以在包含id - rellayout的LinearLayout上使用填充/边距。 (在你的listview_row_layout.xml中)

或者您可以创建一个视图元素来推送该布局上方/下方的空格。 像

//for example 10dp height
<View android:layout_width="match_parent"
      android:layout_height="10dp"
      android:layout_below="@+id/rellayout"/>  

如果你想要等,你也可以给那个视图一个颜色。

个人而言我会在主要版面上获得填充/边距,而不是创建更多的绘制视图,但这是实现目标的另一种方式,我不知道你在做什么以及对你来说最好的方法是什么

答案 1 :(得分:0)

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@color/yourColor"
android:dividerHeight="distnace in dp">