Android:在ScrollView中充气线性视图

时间:2011-05-29 16:38:29

标签: java android android-layout

所以我有一个类从数据库中获取项目,然后将它们放入布局并膨胀。这循环遍历数据库中的每个项目,为每个项目膨胀新布局。这最终会延伸到视口之外,需要一个滚动条,我认为它将是一个ScrollView。我尝试了很多不同的方法,无法滚动浏览整个内容。请指教。

爪哇:

View myView = linflater.inflate(R.layout.myviews, null);

TextView tvQuote = (TextView) myView.findViewById(R.id.lblQuote);
//Set an id to know which quote is in use
tvQuote.setId(i);
TextView tvShared = (TextView) myView.findViewById(R.id.lblShared);
TextView tvSaid = (TextView) myView.findViewById(R.id.lblSaid);

//Change name dynamically
tvQuote.setText((strQuote).toString());
tvShared.setText(("Shared by: " + strFName + " " + strLInitial).toString());
tvSaid.setText(("Said by: " + strFNameSaid + " " + strLInitialSaid).toString());

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        android:orientation="vertical" 
        android:id="@+id/myMainLayout" 
        android:layout_width="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_height="wrap_content">   
    <TextView 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:text="Quoter" 
        android:id="@+id/lblTitle" 
        android:textSize="16px" 
        android:padding="5px" 
        android:textStyle="bold" 
        android:gravity="center_horizontal">
    </TextView>
</LinearLayout>

myviews.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">


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

            <TextView 
                android:id="@+id/lblQuote" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>

        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <TextView 
                android:id="@+id/lblShared" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <TextView 
                android:id="@+id/lblSaid" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_weight="1"/> 
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
          <TextView
            android:text=" "
            android:textSize="1pt"
            android:background="#6F7285"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>     
        </LinearLayout>
</LinearLayout>

滚动浏览所有这些夸大的布局的最佳解决方案是什么?

4 个答案:

答案 0 :(得分:4)

听起来你做的工作太多了。如果您的数据位于SQLite数据库中,那么您可以将SimpleCursorAdapter与ListView一起使用,这将为您带来繁重的工作:

public class MyListActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // The my_list_activity layout is the container for each
        // item in the list.
        setContentView(R.layout.my_list_activity);

        // Create a database cursor for the data you want to query.
        // Assumes your data is accessible via a ContentProvider.

        // Note: db.CONTENT_URI is whatever URI your ContentProvider uses,
        // and db.COL1 etc are constants that would name the columns you want.
        cursor = getContentResolver().query(db.CONTENT_URI,
            new String[] { db._ID, db.COL1, db.COL2, db.COL3 },
            null, null, null);

        // Bind the value in db.COL1 to the resource with the id text1, and so
        // on for the other two columns.
        //
        // Assumes that the view has TextViews with IDs text1, text2, and text3.
        String[] from= new String[] { db.COL1, db.COL2, db.COL3 };
        int[] to = new int[] { R.id.text1, R.id.text2, R.id.text3 };

        // Create the adapter, bound to the cursor, layout, and the mapping from
        // column name to TextView ID.
        //
        // Assumes a layout called my_list_activity_item exists, with at least
        // 3 views called text1, text2, and text3.
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
            R.layout.my_list_activity_item, cursor, from, to);

        setListAdapter(mAdapter);
    }
}

如果您的数据不在SQLite数据库中,那么请查看从android.widget.BaseAdapter派生的其他类,看看其中一个是否适合您。

答案 1 :(得分:0)

您在myviews.xml中使用了太多的线性布局。这应该适合你:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <TextView 
            android:id="@+id/lblQuote" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:text="text"
            />
        <TextView 
            android:id="@+id/lblShared" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@id/lblQuote" 
            android:text="text"
            />
        <TextView 
            android:id="@+id/lblSaid" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@id/lblQuote" 
            android:layout_alignParentRight="true"
            android:text="text"
            /> 
        <TextView
            android:text="text "
            android:background="#6F7285"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/lblShared" 
            />     
    </RelativeLayout>
</ScrollView>

我添加了一些占位符文字,以便您可以看到它。删除任何你不需要的东西。

答案 2 :(得分:0)

我认为快速回答是你需要ScrollView围绕所有这些LinearLayout,所以你可以实际滚动(你的第一个LinearLayout有height = wrap_content)。长版本是你可能做错了,你应该使用ListViewAdapter

答案 3 :(得分:0)

我实际上发现将ScrollView添加到main.xml是修复。感谢你们所有的帮助。我仍然想知道我是否做错了。