简体   繁体   中英

Inflate a view between elements in a view (Android XML)

I want to inflate some views but have a button at bottom of the view. I already know how to dynamically inflate views and then inflate the button below the other inflated views. However, when there are only a few inflated views (causing the combined view to take less space than an entire screen) the button does not rest at the bottom of the page as i would like. My idea is that i need to have the button aligned at the bottom of the relative layout by setting the button to: android:layout_alignParentBottom (when i try to do this though the app crashes)

here is a screenshot of what i have, followed by a screenshot of what i want, all the items you see are inflated onto a scrollview that is inside a relative layout. I think the problem is that the button is inflated onto the scrollview which is only as large at the items force it to be (unfortunately I'm too noob to know how to inflate onto a different part of the view)

在此处输入图片说明

the code for the image on the left is as follows (i dont have and errors for the view on the left): parentView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/blurybg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    
<ScrollView     
  android:layout_height="wrap_content" 
  android:id="@+id/scrollView1" 
  android:layout_width="fill_parent" >
<LinearLayout     
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id= "@+id/parent_of_inflated_view_id">
</LinearLayout>   
</ScrollView>
</RelativeLayout>

childButton (other views are similar to this)

<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="30dp"
android:layout_width="fill_parent"
android:background="@drawable/done"
android:layout_alignBottom ="@layout/availablerestaurants"
/>

Java for creating the inflator and inflating the button. other inflated views are similar to this (i use the same inflator of course). I am adding the inflated button last to get it at the bottom of the view.

LayoutInflater theInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout linLayout = (LinearLayout)findViewById(R.id.parent_of_inflated_view_id); 

    Button doneButtonCurrentRest=  (Button) theInflater.inflate(R.layout.done_button_availrest, null);
    linLayout.addView(doneButtonCurrentRest);

Thanks for the help, Adam

Please check my code. It should work as intended:

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

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

        <LinearLayout
            android:id="@+id/parent_of_inflated_view_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

What about your code. I can see several mistakes:

1) You should specify android:layout_height for your button. Without this parameter exception will be thrown. You use android:height but it is just not the same as android:layout_height .

2) android:layout_alignBottom should point to the id, but not to the layout. Moreover, i don't see the purpose of this attribute in the separate layout for button.

Update

Here is the solution for the question in the comment below:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:id="@+id/layout_for_positioning"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/parent_of_inflated_view_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_weight="0" />
    </LinearLayout>

</ScrollView>

Note that i used android:fillViewport="true" for the ScrollView so it can fill the entire screen. Also i used android:layout_weight to stretch views properly (you can find the explanation of the attribute here ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM