我想要更改当前布局,以便我可以滚动整个布局。

时间:2017-02-09 03:11:29

标签: android

<LinearLayout
    android:id="@+id/layout_question_detail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:visibility="gone">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        >
        <Button
            android:id="@+id/btn_question_backButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/backicon_round"/>

        <TextView
            android:id="@+id/text_question_detail_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:textColor="@color/color_green"
            android:text="Error"
            android:textSize="19dp"

            />
    </RelativeLayout>

    <ImageView
        android:id="@+id/image_question_detail_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:scaleType="centerCrop"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="2">

        <TextView
            android:id="@+id/text_question_detail_userComments"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:layout_marginBottom="25dp"
            />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3">

            <ListView
                android:id="@+id/list_question_detail_expComments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@android:color/transparent"
                android:dividerHeight="10dp"></ListView>

        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

现在,我将标题的布局设置在顶部。然后,Imageview,textview和listview遵循此布局,并且listview中只有1个textview。 listview的大小可能会有所不同。

问题是,如果listview的大小非常大,我只能滚动分配给listview的屏幕。 但是,我想滚动整个屏幕。

为了解决这个问题,我在第一个linearlayout之外添加了scrollview。 然而,它没有用。(图像视图消失了)

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以制作listview addheaderview,并将您的imageview和textview放入headerview

yourlayout.xml

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


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="55dp">

    <Button
        android:id="@+id/btn_question_backButton"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/text_question_detail_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Error"
        android:textColor="#000000"
        android:textSize="19dp"

        />
</RelativeLayout>


<ListView
    android:id="@+id/list_question_detail_expComments"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@android:color/transparent"
    android:dividerHeight="10dp"></ListView>

lv_header.xml

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


<ImageView
    android:id="@+id/image_question_detail_image"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:scaleType="centerCrop" />


<TextView
    android:id="@+id/text_question_detail_userComments"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="25dp"
    android:layout_marginTop="10dp"
    android:layout_weight="1"
    android:textSize="20dp" />

Activity.class

public class MainActivity extends AppCompatActivity {

private ListView list_question_detail_expComments;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list_question_detail_expComments = (ListView) findViewById(R.id.list_question_detail_expComments);
    View lvHeaderView = View.inflate(this,R.layout.lv_header,null);
    list_question_detail_expComments.addHeaderView(lvHeaderView);
    list_question_detail_expComments.setAdapter(new LvAdapter());

}

private class LvAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return 20;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

            TextView tv = new TextView(MainActivity.this);

             tv.setText("test"+i);

        return tv;
    }
}

}

相关问题