我是新來的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);
}
}
任何幫助,將不勝感激
使用相對佈局而不是LinearLayout。 –