【发布时间】:2016-12-27 09:34:00
【问题描述】:
我是 Android 开发的新手,我认为我错过了一些小而重要的事情(或者我正在做一些完全错误的事情)。 我创建了一个添加了许多控件(工具栏、自定义视图和两个按钮)的屏幕布局。 目前自定义视图所做的只是在屏幕上绘制文本。但是,它正在做一些我没想到的事情。
首先,文本在工具栏下方绘制。这没什么大不了的,因为我可以偏移绘图以使其显示在屏幕上,但由于我的工具栏和视图处于垂直方向的线性布局中,我希望我的自定义视图立即在工具栏下方开始。
无论如何,我的主要问题是,当我的自定义视图添加到布局时,它似乎会在整个屏幕上绘制,因此我看不到这两个按钮(如果我删除我的自定义视图,按钮就在那里)。
这是活动的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.myapp.MainScreen">
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<com.myapp.MyControl
android:id="@+id/my_control"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id = "@+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button android:id = "@+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
此刻我的控制很简单:
public class MyControl extends View {
private String mText;
private Paint mTextPaint;
public MyControl(Context context, AttributeSet attributeSet){
super(context, attributeSet);
mText = "My Control";
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTextSize(100);
}
protected void onDraw(Canvas canvas)
{
canvas.drawText(mText, 5, 50, mTextPaint);
}
}
任何帮助将不胜感激
【问题讨论】:
-
使用相对布局而不是线性布局。