2016-08-20 48 views
0

我是新來的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); 
    } 
} 

任何幫助,將不勝感激

+0

使用相對佈局而不是LinearLayout。 –

回答

0

使用此

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/main_activity" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="1"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.75"> 

      <com.thisana.RageMaker.Helpers.MyControl 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/view" 
       android:layout_gravity="center_horizontal"/> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.25" 
      android:weightSum="1" 
      android:gravity="center" 
      > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="New Button" 
       android:id="@+id/button"/> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="New Button" 
       android:id="@+id/button2"/> 
     </LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 

enter image description here

+0

這似乎不太有效。 是的,我現在可以看到我的按鈕,但我的視圖不會畫到屏幕上。 請注意,我在最後添加了上面缺少的。 –

+0

sry代碼對齊問題答案更新。 –

+0

沒有變化。我的觀點中的文字仍然沒有畫到我的屏幕上。 onDraw方法肯定被調用,但沒有任何繪製 –