2

我想每个屏幕都适合屏幕的每个项目/行。但是我在 dp 中为行的 xml 指定高度和宽度来实现它。正弦我指定高度/宽度,每一行都不完全适合屏幕,也不适合所有类型的屏幕分辨率。我将如何实现这一目标?有人可以对此提出想法吗?

这是xml文件:

   <?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> 

提前致谢。

4

3 回答 3

1

请利用线性布局,通过使用 weightsum 的功能,您可以将屏幕分辨率划分到任何程度...

于 2013-01-05T12:23:05.377 回答
0

您是否使用过LazyAdapter

尝试这个...

演示.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;
}
}

惰性适配器布局.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>

希望这对你有帮助!!!

有关更多详细信息,请参阅本教程。

于 2013-01-05T05:39:34.613 回答
0

您是否尝试过这两个而不是指定 dpi

    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
于 2013-01-05T05:18:50.823 回答