シンプルなカスタムビューを作成する方法は?
-
09-10-2019 - |
質問
カスタムを作成したいと思います View
Androidで。私はできるだけ単純なことをしようとし、ほとんど空のクラスを作成しました MyView
そして私の中でそれを使用しました LinearLayout
しかし、アプリケーションは「フォースクローズ」で開始時に失敗します。簡単なカスタムを行うにはどうすればよいですか View
?によると カスタムコンポーネントの構築 View
オーバーライドしない場合は、サイズ100x100を取得します onMeasure()
.
public class MyView extends View {
public MyView(Context context) {
super(context);
}
}
そして、私はそれをaで使用します LinearLayout
と:
<view
class="com.example.MyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
私は何が間違っているのですか?
コンストラクターを使用する場合 itemon 提案とスーパークラスへの対応する呼び出し。その後、「フォースクローズ」はなくなりましたが、 LinearLayout
後のコンポーネントが壊れています MyView
表示されていません。
これが私のものです main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:background="#f00"
android:text="Hello"
/>
<view
class="com.example.MyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:background="#00f"
android:text="World"
/>
</LinearLayout>
解決
次のような別のコンストラクター方法を定義できます。
public MyView(Context context, AttributeSet attrs)
Androidフレームワークは、上記のコンストラクターからのビューでUIを構築しようとします。
他のヒント
Android Developer Guideには、Building Custom Componentsというセクションがあります。残念ながら、XML属性の議論は、レイアウトファイル内のコントロールを宣言するだけであり、クラスの初期化内の値を実際に処理しないことのみをカバーしています。手順は次のとおりです。
values attrs.xmlで属性を宣言します
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyCustomView">
<attr name="android:text"/>
<attr name="android:textColor"/>
<attr name="extraInformation" format="string" />
</declare-styleable>
</resources>
declare-stylaibleタグで資格のない名前の使用に注意してください。標準以外のAndroid属性のような標準のAndroid属性は、それらのタイプを宣言する必要があります。スーパークラスで宣言されたタグは、再宣言することなくサブクラスで利用できます。
コンストラクターを作成します
初期化に属性セットを使用する2つのコンストラクターがあるため、コンストラクターが呼び出すための個別の初期化方法を作成するのが便利です。
private void init(AttributeSet attrs){
TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MyCustomView);
//Use a
Log.i("test",a.getString(R.styleable.MyCustomView_android_text));
Log.i("test",""+a.getColor(R.styleable.MyCustomView_android_textColor, Color.BLACK));
Log.i("test",a.getString(R.styleable.MyCustomView_android_extraInformation));
//Don't forget this
a.recycle();
}
R.Styleable.MycustomViewは、各要素が属性のIDである自動生成int []リソースです。属性は、属性名を要素名に追加することにより、XMLの各プロパティに対して生成されます。属性は、さまざまなGET関数を使用してTypedArrayから取得できます。属性がXMLで定義されていない場合、nullが返されます。もちろん、返品タイプが原始的である場合、その場合、2番目の引数が返されます。
すべての属性を取得したくない場合は、この配列を手動で作成することができます。標準のAndroid属性のIDはandroid.r.attrに含まれていますが、このプロジェクトの属性はR.Attrにあります。
int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor};
android.r.styleableで何も使用してはいけないことに注意してください。このスレッドによると、将来変更される可能性があります。これらのすべての定数を1つの場所で表示することは、まだ文書にあります。
レイアウト main.xmlなどのレイアウトファイルで使用します。名前空間宣言を含める
xmlns:app = "http://schemas.android.com/apk/res/com.mycompany.projectname"
上部レベルXML要素。
<com.mycompany.projectname.MyCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="Test text"
android:textColor="#FFFFFF"
app:extraInformation="My extra information";
/>
完全資格のある名前を使用してカスタムビューを参照してください。
Android LabelViewサンプル
完全な例が必要な場合は、Androidラベルビューサンプルをご覧ください。
labelview.java
TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView);
CharSequences=a.getString(R.styleable.LabelView_text);
attrs.xml
<declare-styleable name="LabelView">
<attr name="text"format="string"/>
<attr name="textColor"format="color"/>
<attr name="textSize"format="dimension"/>
</declare-styleable>
custom_view_1.xml
<com.example.android.apis.view.LabelView
android:background="@drawable/blue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:text="Blue"app:textSize="20dp"/>
これは、名前空間属性を備えたLinearLayoutに含まれています。
xmlns:app = "http://schemas.android.com/apk/res/com.example.android.apis"