2010-12-28 12 views
1

私はAndroidの初心者で、グラフィックスパートでは苦労します。 XMLファイルでレイアウトを作成することの美しさを理解しましたが、さまざまな要素へのアクセス方法、特にその要素に描画するView要素が失われました。Androidプログラミング - コードを使用してmain.xmlレイアウトのXMLビューにアクセスする方法

私のレイアウトの例を参照してくださいmain.xmlここに、

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

      <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/Title" android:text="App title" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#000000" 
       android:background="#A0A0FF"/> 
    </LinearLayout> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/PaperLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:focusable="true"> 

      <View 
       android:id="@+id/Paper" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 
    </LinearLayout> 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

      <Button 
       android:id="@+id/File" 
       android:layout_width="fill_parent" 
       android:layout_weight="1" 
       android:layout_height="34dp" 
       android:layout_alignParentTop="true"    
       android:layout_centerInParent="true" 
       android:clickable="true" android:textSize="10sp" 
       android:text="File" /> 

     <Button 
       android:id="@+id/Edit" 
       android:layout_width="fill_parent" 
       android:layout_weight="1" 
       android:layout_height="34dp" 
       android:clickable="true" 
       android:textSize="10sp" android:text="Edit" /> 
    </LinearLayout> 
</LinearLayout> 

あなたは私が底にカスタムアプリtitle bar、その後、View充填ミドル、そして最後に2 button秒を持って見ることができるように。例えば、タイトルバーのテキストを変更し、View背景色を変更する、button s'のイベントをキャッチし、への応答は正常に動作しますが、どのように一体私はアクセスし、より重要なのmain.xml

UPDATEで定義されたビュー上に描画します: 感謝あなたは非常にあなたの提案のために、それ以外にViewImageViewではなく、canvas.drawText()と終了括弧にパラメータがありません、それは動作しません。

これは、私が初心者であり、空白を記入できると仮定したという事実を忘れたためです。

今私のmain.xmlレイアウトファイルで、私は私が必要なものであるViewあるいはSurfaceView要素を作成することができますが、あなたのソリューションに応じて、私も<View ....../>のようなビューを指定していない理由をまず第一に私は理解していません

とにかく私のmain.xmlはあなたのソリューションに従って編集されています。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

     <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/Title" android:text="App title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#000000" 
      android:background="#A0A0FF"/> 
    </LinearLayout> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/PaperLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:focusable="true"> 

      <com.example.MyApp.CustomView 
       android:id="@+id/Paper" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 
      <com.example.colorbook.CustomView/> 

    </LinearLayout> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

      <Button 
       android:id="@+id/File" 
       android:layout_width="fill_parent" 
       android:layout_weight="1" 
       android:layout_height="34dp" 
       android:layout_alignParentTop="true" 
       android:layout_centerInParent="true" 
       android:clickable="true" 
       android:textSize="10sp" 
       android:text="File" /> 
    </LinearLayout> 
</LinearLayout> 

私のメインのJavaファイルMyApp.javaにあります。私はこれをonCreate()の後に追加しました。

public class CustomView extends ImageView { 
    @Override 
    protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      canvas.drawText("Your Text", 1, 1, null); 
    } 
} 

しかし、私はのCustomView部分にエラーが発生します。 「デフォルトconstructor.Mustは明示的なコンストラクタを定義するための暗黙的なスーパーコンストラクタImageView()が未定義である」

Eclipseは、コンストラクタを追加し、約3つのクイックフィックスを提案したが、どれも、助けていないだけでなく、それはエラーが削除されますが実行しているときにアプリ上でエラーが発生します。

誰かが私のためにこれを打ち破り、解決策を提供し、私のmain.xmlレイアウトファイルにView要素を作成してコードで描画できない理由を説明してくれることを願っています。

更新: [OK]まだ解決策を使用しようとしていますが、まだ問題とさまざまなエラーが発生しています。しかし、まず第一に、私はlayoutで使用するカスタムクラスを作成する必要がある理由を理解できません。これは、wysiwygレイアウトエディタを使用してView要素を挿入して、今はコードファイルだけです。だから、誰かが私に答えてくれます。

とにかく私は持っています。

package com.example.myapp; 

import com.example.myapp.CustomView; 
//and here comes other import 

public class MyApp extends Activity { 
//rest of code including OnCreate() 

新しいファイルCustomView.Javaを作成しました。ここにコードを直接貼り付けています。その後、

public class CustomView extends ImageView { 
    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Paint paint = new Paint(); 
     paint.setARGB(255, 150, 205, 129); 
     paint.setTextSize(20); 
     canvas.drawText("Your Text", 1, 1, paint); 
    } 
} 

[OK]をので、このクラスコードは、私はImageViewためimport秒を追加したい、CanvasPaint、私がやった、と再び私は*CustomView*一部戻ってエラーにpublicクラスです。 "暗黙のスーパーコンストラクタImageView()は、デフォルトのコンストラクタでは未定義です。明示的なコンストラクタを定義する必要があります。"

ご協力いただきありがとうございます、ありがとうございます!

+0

あなたのタイトルに「Androidプログラミング」の必要はありません。 –

+0

ありがとうございましたが、タグ "プログラミング" – DKDiveDude

+0

を使用することはできませんでしたが、もちろん、私は提供されたソリューションをテストした後、答えを受け入れてマークします。それはあなたと大丈夫ですか、私はそれが動作する前に、私が答えを受け入れることを好むか? – DKDiveDude

答えて

0

活動にmain.xmlに定義されたビューを使用するには、次のような活動のonCreateメソッドをオーバーライドします。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

}

編集:申し訳ありませんが、私は、「ビューの上に描く」読み違えドロー」などあなたの質問で "見ることができます:) L0rdAli3nの答えを参照してください。

+0

返信ありがとう、その部分は私が知っている;)そして、私が書いたように、私はビューの要素の背景色を変更することができますが、実際にそのビューにグラフィックスを描画するにはどうすればいいですか? – DKDiveDude

0

onCreateメソッドをオーバーライドしてからfindViewByIdメソッドを使用します。

View v = (View)findViewById(R.id.Paper); 
v.setBackgroundColor(myColor); 

同様に他のビュー/ウィジェットを変更するには、独自のカスタムビューをクリート、ビュー上に描画するには、あなたのレイアウトに

+0

また、ご返信ありがとうございます。あなたとAxarydaxが示唆していることの両方を行うことによって、実際に私が実際に背景色を変えることができるということは私の質問ではっきりとしていないようです。私の本当の疑問は今どのように私はそのビュー上にグラフィックを描くのですか? – DKDiveDude

+0

私はL0rdAli3nが上の投稿で言ったことと一緒に行くだろう – pankajagarwal

3

定義:ビューの背景色は、次のコマンドを使用使用して変更するたとえば、

これを行うには、新しいクラスを追加する必要があります。日食では、それは単なるファイル - >新 - >クラスであり、そこに行く。 onDraw()をオーバーライドします - あなたのニーズに合わせて、あなたのレイアウトにあなたのCustomViewを追加する方法:このようなあなたのCustomViewを追加してXMLファイル内

public class CustomView extends ImageView { 
    // The needed constructors: 
    public CustomView(Context context) { 
     super(context); 
    } 
    public CustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 
    public CustomView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    }

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    Paint paint = new Paint(); 
    paint.setARGB(255, 150, 205, 129); 
paint.setTextSize(20); 
    canvas.drawText("Your Text", xposition, yposition, paint); 
} 

}

<com.example.CustomView 
      android:id="@+id/yourID" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentBottom="true" 
      android:layout_margin="10dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     </com.example.CustomView> 

res-Folderからドロアブルに描画したくない場合は、customViewの "src"パラメータを指定します。

+0

あなたの提案のために、それ以外にもImageViewではなくViewが必要で、あなたは終わり括弧がないので、うまくいきません。 – DKDiveDude

+0

さて、あなたは自分のアプリを書くためにいくつかのJavaの基本を学ばなければならないのです...私は物事を明確にするために私の答えを改善します。我慢して。申し訳ありませんがブラケットがありません、私は私の例をスリム化し、多くslmied :) –

+0

あなたが言ったように、描画することはできませんので、あなたがビューまたはイメージビューで描画する場合気にする必要はありません結果は同じです。 –

関連する問題