Android:ScrollView和RelativeLayout无法正常工作

时间:2014-08-08 12:27:31

标签: android android-layout android-scrollview

我制作了一个布局,我需要添加一个除了所有组件之外的scrollview。正如您在下面的图像中看到的那样,由于某种原因我放置所有组件的RelativeLayout不会填充父级而是包装内容。但这会影响ExpandableListView,其中只显示一个没有子项的组项。我希望我的RelativeLayout填充父级不要换行。您可能会说可扩展列表可能会导致这种情况,但我尝试了没有它,并再次包装内容而不是填充父级。

activity_product.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


        <com.daimajia.slider.library.SliderLayout
           android:id="@+id/slider"
           android:layout_width="match_parent"
           custom:pager_animation="Default"
           custom:auto_cycle="true"
           custom:indicator_visibility="visible"
           custom:pager_animation_span="1100"
           android:layout_alignParentLeft="true"
           android:layout_height="200dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="-35%"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:textSize="25dp"
        android:background="@color/orange"
        android:layout_marginBottom="71dp" />

    <ExpandableListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/expandableListView"
        android:layout_below="@+id/slider"
        android:layout_alignParentLeft="true" />


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

截图 enter image description here

1 个答案:

答案 0 :(得分:2)

请尝试这种方式,希望这有助于您解决问题。

<强> activity_main.xml中

<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/expandableListView"/>

<强> header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="-35%"
        android:id="@+id/textView"
        android:textSize="25sp"
        android:background="@color/orange"/>

    <com.daimajia.slider.library.SliderLayout
        android:id="@+id/slider"
        android:layout_width="match_parent"
        custom:pager_animation="Default"
        custom:auto_cycle="true"
        custom:indicator_visibility="visible"
        custom:pager_animation_span="1100"
        android:layout_height="200dp"/>
</LinearLayout>

<强> MainActivity.java

public class MainActivity extends Activity {

    private ExpandableListView expandableListView;
    private TextView textView;
    private SliderLayout slider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

        View header = LayoutInflater.from(this).inflate(R.layout.header,null);
        textView = (TextView) findViewById(R.id.textView);
        slider = (SliderLayout) findViewById(R.id.slider);
        expandableListView.addHeaderView(header);
        expandableListView.setAdapter(YourExpandableListViewAdapterHere);

    }
}