简体   繁体   中英

Android: fit each item of a listview per screen

I want to fit each item/row of the screen per screen. But I am specifying height and width in dp for xml of row for implementing that. Sine I am specifying height/width, each row is not fitting exactly the screen and also not for all type of screen resolution. How I will achieve that?Can anybody please give idea on that?

Here is the xml file:

   <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="1024dip"
android:layout_height="480dip"
android:orientation="vertical" >
<TextView android:layout_width="1024dip"
android:layout_height="480dip" 
android:text="ViewSwitcher" 
android:textSize="20sp" 
android:gravity="center" 
android:padding="10dip" 
android:background="#404040" 
android:id="@+id/textView" /> 
</LinearLayout> 

Thanks in advance.

请使用权重和的特征来利用线性布局,通过它,您可以在任何程度上划分屏幕分辨率...

Have you tried these two instead of specifying the dpi's

    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"

Have you worked with LazyAdapter .

Try this...

Demo.java:

public class Demo extends Activity {

LazyAdapter adapter;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo_layout);

    lv = (ListView) findViewById(R.id.list);
    ArrayList<String> songsList = new ArrayList<String>();

        songsList.add("Song 1");
        songsList.add("Song 2");
        songsList.add("Song 3");

        adapter=new LazyAdapter(this, songsList);        
        lv.setAdapter(adapter);
}   

 public void onListItemClick(ListView l, View v, int position, long id) {

        //Do Something here
    }

}

demo_layout.xml:

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

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="#b5b5b5"
    android:textSize="20sp"
    android:dividerHeight="1dp"/>

</RelativeLayout>

LazyAdapter.java:

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<String> data;
private static LayoutInflater inflater=null;

public LazyAdapter(Activity a, ArrayList<String> d) {

    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)

    vi = inflater.inflate(R.layout.lazyadapter_layout, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title

    String song;
    song = data.get(position);

    // Setting all values in listview
    title.setText(song);

    return vi;
}
}

lazyadapter_layout.xml:

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="480dp"
        android:background="#404040"
        android:gravity="center"
        android:text="ViewSwitcher"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

</LinearLayout>

Hope this helps you !!!

For more details refer this tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM