簡體   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