简体   繁体   中英

how to configure correctly a View in LinearLayout

This is a small app that should draw different elements. On the top are the buttons to select the forms and below the drawing space. It draws but it's putting the drawing over the buttons ... so I can't access them. Here is the xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >

<LinearLayout android:layout_width="wrap_content" 
    android:layout_height="wrap_content">

<!--First line of buttons -->

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
<!--2nd line of buttons -->

</LinearLayout>

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" />

</LinearLayout>
</LinearLayout>

The main class:

package com.example.drawsomething;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DrawSome extends Activity implements OnClickListener {

DrawCanvas canvas;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawsome);

    Button cerc=(Button) findViewById(R.id.button1);
    cerc.setOnClickListener(this);
            <!-- all the other buttons-->

    View vw=findViewById(R.id.view1);

    canvas=new DrawCanvas(this);
    addContentView(canvas, vw.getLayoutParams()); // ?!? probably this is the problem
}


@Override
public void onClick(View v) {
    if(v.getId()==R.id.button1){
        canvas.getShape(DrawCanvas.CERC);
    }
<!--onClick for the rest of the buttons-->
}
}

Have you tried putting a margin in the LinearLayout that contains the view? Just a thought. The reason I say this is that the LinerLayout below overlaps the button layouts in eclipse. XML viewer.

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="10dp" >

<View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" />

</LinearLayout>

When I made a project at Uni I had a similar issue. It is simply a 3D model loader, I used this XML. With this I managed to get the buttons to be in front of the model. I think you may have to do this pragmatically unfortunately.

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

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

        <LinearLayout
            android:id="@+id/scene2Holder"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
             >
        </LinearLayout>

        <ImageButton
            android:id="@+id/MoveDown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="3dp"
            android:layout_toLeftOf="@+id/MoveRight"
            android:background="@null"
            android:src="@drawable/mapdown" />

        <ImageButton
            android:id="@+id/MoveLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="33dp"
            android:layout_toLeftOf="@+id/MoveDown"
            android:background="@null"
            android:src="@drawable/mapleft" />

        <ImageButton
            android:id="@+id/MoveUp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/MoveLeft"
            android:layout_toRightOf="@+id/MoveLeft"
            android:background="@null"
            android:src="@drawable/mapup" />

        <ImageButton
            android:id="@+id/ZoomOut"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="33dp"
            android:layout_marginLeft="3dp"
            android:background="@null"
            android:src="@drawable/mapzoomout" />

        <ImageButton
            android:id="@+id/imageButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="45dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_toRightOf="@+id/ZoomOut"
            android:background="@null"
            android:src="@drawable/mapline" />

        <ImageButton
            android:id="@+id/ZoomIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="33dp"
            android:layout_toRightOf="@+id/imageButton6"
            android:background="@null"
            android:src="@drawable/mapzoomin" />

        <ImageButton
            android:id="@+id/MoveRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="33dp"
            android:background="@null"
            android:src="@drawable/mapright" />
    </RelativeLayout>

</LinearLayout>

And achieved:

在此处输入图片说明

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