0

Android アプリでカスタム プロパティを定義したいと考えています。

View を拡張する GifMovieView クラスを定義しました。

3 つのコンストラクターはすべて init(AttributeSet attrs) メソッドを呼び出します。

if (attrs != null) {
        String packageName = "http://schemas.android.com/apk/res/android";
        mUrl = attrs.getAttributeValue(packageName, "url");
        Log.i("TAG_MIO", mUrl);
    }

mUrl が null であるため、Log.i は例外をスローします。

このプロパティは、mainActivity_layout.xml で次のように定義されています。

 <com.example.propertyproject.GifMovieView
    android:id="@+id/my_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    gif:url="http://mygif.com/mygif.gif" />

URLは常にnullです...

パッケージ名は間違っていますか?packageName を変更する必要がありますか?

事前にTY、

アドリアーノ。

4

2 に答える 2

0
  1. 値フォルダーに attrs.xml ファイルを作成します。これには、すべてのカスタム属性が含まれます

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <declare-styleable name="MyTextView">
        <attr name="font_name" format="string" />
    </declare-styleable>
</resources>

  1. 値フォルダにstyles.xmlを作成します. 特定のコンポーネントのスタイルが含まれます. ここではTextviewの例を取り上げます. TextViewのフォント名属性に移動します.

<style name="Header_Text_Text1">
        <item name="android:textColor">@color/text1_header_text</item>
        <item name="android:textSize">26px</item>
        <item name="android:textStyle">bold</item>
        <item name="android:padding">2dp</item>
        <item name="[YOUR PACKAGE NAME]:font_name">calibri</item>
    </style>

  1. 新しい属性を使用する Java ファイルを作成しましょう。Textview を拡張し、mytextview.java を作成します。

public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    if (isInEditMode())
        return;

    final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MusicTextView);
    final String ttfName = ta.getText(0).toString();
    Log.i("Font Name :" + ttfName);
    ta.recycle();
}

}

  1. 新しい「MyTextview」という名前の新しい Textview をレイアウトで使用できます。

<YOUR PACKAGE NAME.MyTextView
        android:id="@+id/header_txtTitle"
        style="@style/Header_Text_Text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/header_home"
        android:layout_marginTop="2dp"
        android:text="" />

于 2012-09-11T08:58:58.823 に答える
0

*_layout.xmlカスタム文字列を定義するためではなく、GUI のレイアウトを定義するために使用されます。

そのために予約された別の場所がありますres/values/strings.xmlstrings.xml存在しない場合は、次の内容で作成します。

<?xml version="1.0" encoding="utf-8"?>
<resources>    
  <string name="gif_url">http://mygif.com/mygif.gif</string>
</resources>

次に、次を使用してアクセスできます

String url = R.string.gif_url;  // (it becomes a static final property)
于 2012-09-11T08:29:47.807 に答える