繁体   English   中英

如何在LinearLayout中正确配置视图

[英]how to configure correctly a View in LinearLayout

这是一个小应用程序,应该绘制不同的元素。 顶部上方是用于选择表格的按钮,而下方是绘图空间。 它可以绘制,但是可以将其放置在按钮上...所以我无法访问它们。 这是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>

主班:

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-->
}
}

您是否尝试过在包含视图的LinearLayout中放置边距? 只是一个想法。 我之所以这样说是因为下面的LinerLayout与eclipse中的按钮布局重叠。 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>

当我在Uni进行项目时,我遇到了类似的问题。 它只是3D模型加载器,我使用了此XML。 这样,我设法使按钮位于模型的前面。 我认为您可能不幸地必须这样做。

<?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>

并实现:

在此处输入图片说明

暂无
暂无

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

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