简体   繁体   English

Android Button和可绘制区域

[英]Android Button and drawable area

I am novice on Android programming. 我是Android编程的新手。 My task is simple: I want to create a screen with just two objects: 我的任务很简单:我想创建一个只有两个对象的屏幕:

  • an action Button; 一个动作按钮;
  • a drawable area in wich to draw images, text, circles, and so on. 一个用于绘制图像,文本,圆圈等的可绘制区域。

Is it possible so have a working example, or at least a guideline ? 是否有可能提供一个可行的示例或至少一个指南?

I know how to subclass a view, and to draw int it, using: 我知道如何使用以下方法来继承视图并对其进行绘制:

    MyView d = new MyView(this);
    setContentView(d);

But this fills all the screen with MyView and the button is not visible. 但这会用MyView填充所有屏幕,并且该按钮不可见。 Some suggestions ? 有什么建议吗?

You need to define a layout file and specify relative position of button and the drawable area. 您需要定义一个布局文件,并指定按钮和可绘制区域的相对位置。 Make sure both of them are not specified as fill_parent in layout_width or layout-height. 确保两个都未在layout_width或layout-height中指定为fill_parent。

Set the contentView to this layout file 将contentView设置为此布局文件

You go through android Ui for detail understanding. 您通过android Ui进行了详细了解。

When you create an android project in eclipse you'll find res/layout/main.xml and this is where your default UI is defined and that is set using setContentView(R.layout.main); 当您在eclipse中创建一个android项目时,您会找到res / layout / main.xml,这是定义默认UI的地方,并且使用setContentView(R.layout.main)进行设置; in onCreate method. 在onCreate方法中。

To put images you can use imageview and textview for Texts in xml. 要放置图像,您可以对XML中的文本使用imageview和textview。 Like that many widgets are there for edit text, Button etc. A simple example including Imageview,textview and Button: 像这样,有许多小部件可用于编辑文本,Button等。一个简单的示例包括Imageview,textview和Button:

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

  <TextView 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="HELLO ANDROID"
 android:layout_gravity="center"
 />

    <Button 
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 android:text="Click Me"
 />

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

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