私は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で定義されたビュー上に描画します: 感謝あなたは非常にあなたの提案のために、それ以外にView
、ImageView
ではなく、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
秒を追加したい、Canvas
とPaint
、私がやった、と再び私は*CustomView*
一部戻ってエラーにpublicクラスです。 "暗黙のスーパーコンストラクタImageView()は、デフォルトのコンストラクタでは未定義です。明示的なコンストラクタを定義する必要があります。"
ご協力いただきありがとうございます、ありがとうございます!
あなたのタイトルに「Androidプログラミング」の必要はありません。 –
ありがとうございましたが、タグ "プログラミング" – DKDiveDude
を使用することはできませんでしたが、もちろん、私は提供されたソリューションをテストした後、答えを受け入れてマークします。それはあなたと大丈夫ですか、私はそれが動作する前に、私が答えを受け入れることを好むか? – DKDiveDude