简体   繁体   中英

Add buttons to layout with a View

I'm trying out the Finger Paint API demo and I'm trying to add buttons to it. I have included

<?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="match_parent"
    android:orientation="vertical" >

    <view
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.triopsys.imosandroid.ViewControllers.SignatureViewController$MyView" />

</LinearLayout>

In my layout xml, but I can't add buttons on the top or bottom of this view;

<?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="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/buttonCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel" >
        </Button>

        <Button
            android:id="@+id/buttonOk"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <view
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.triopsys.imosandroid.ViewControllers.SignatureViewController$MyView" />
    </LinearLayout>

</LinearLayout>

But this doesn't work.

Try adding

android:orientation="horizontal"

to the Layout wrapping your buttons. IIRC the default is vertical, so it's stacking 2 buttons with zero width on top of each other.

If you are getting no errors:

then I guess the problem is with the Buttons' width values. Change it to wrap_content

What exactly doesn't work? I tried this layout, it compiles and correctly displays.

If "doesn't work" means "my custom view is not takes the whole screen" then remove linearLayout2 . In any case it is redundant and may be deleted.

<?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="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/buttonCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel" >
        </Button>

        <Button
            android:id="@+id/buttonOk"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK" >
        </Button>
    </LinearLayout>

    <view
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.triopsys.imosandroid.ViewControllers.SignatureViewController$MyView" />

</LinearLayout>

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