简体   繁体   English

如何在LinearLayout中正确配置视图

[英]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: 这是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? 您是否尝试过在包含视图的LinearLayout中放置边距? Just a thought. 只是一个想法。 The reason I say this is that the LinerLayout below overlaps the button layouts in eclipse. 我之所以这样说是因为下面的LinerLayout与eclipse中的按钮布局重叠。 XML viewer. XML查看器。

<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. 当我在Uni进行项目时,我遇到了类似的问题。 It is simply a 3D model loader, I used this XML. 它只是3D模型加载器,我使用了此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: 并实现:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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