如何在ListView中创建一行调整到Android应用程序中的内容

时间:2014-05-29 22:03:50

标签: java android listview android-listview

好的,现在我有一个具有设定高度的ListView。

good

但是当ListView的某一行内的内容增长时......行没有

bad

正如您在第二张图片上看到的那样,内容在第一行被截断

当没有足够的空间时,有什么方法可以让单行扩展?

活动代码:

public class MainActivity extends ActionBarActivity {

ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    // Construct the data source
    arrayOfLocations = new ArrayList<Location>();

    // Create the adapter to convert the array to views
    adapter = new LocationAdapter(this, arrayOfLocations);

    adapter.add(new Location(R.drawable.ic_launcher, "Fruit Stand",
            "We have the freshest fruit in the whole world!", "2 miles",
            "8-5 mon-sat\nclosed sun"));

    //getData();

    // Attach the adapter to a ListView
    ListView listView = (ListView) findViewById(R.id.listView1);

    View header = (View) getLayoutInflater().inflate(
            R.layout.listview_header, null);
    listView.addHeaderView(header);

    listView.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

行的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="140dip" >

<ImageView
    android:id="@+id/imgIcon"
    android:layout_width="90sp"
    android:layout_height="120sp" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_toRightOf="@id/imgIcon"
    android:orientation="vertical"
    android:paddingLeft="10sp" >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tvDetails"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/tvDistance"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/tvHours"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />
</LinearLayout>

</RelativeLayout>

非常感谢你!

1 个答案:

答案 0 :(得分:5)

可能是您应该将行高更新为“wrap_content”而不是140dip

<?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="wrap_content"
    android:minHeight="140dip" >
    ...
</RelativeLayout>