2

実行時にAndroidプラットフォーム用のその他のウィジェットからパネルを作成したいと思います。

 XmlPullParser parser = getResources().getXml(R.xml.panel_attribute);
 AttributeSet attributes = Xml.asAttributeSet(parser);
 Panel panel = (Panel) new Panel(getActivity(),attributes);

panel_attribute.xmlは何である必要がありますか?

パネルは次のようになります

<org.miscwidgets.widget.Panel
    xmlns:panel="http://schemas.android.com/apk/res/org.miscwidgets"
    android:id="@+id/topPanel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="4dip"
    panel:animationDuration="1000"
    panel:closedHandle="@drawable/sliding_drawer_handle_minimized"
    panel:content="@+id/searchparams_layout"
    panel:handle="@+id/handle"
    panel:linearFlying="true"
    panel:openedHandle="@drawable/sliding_drawer_handle_minimized"
    panel:position="top" />
4

1 に答える 1

1

まず、レイアウトファイル内と同様に、xmlフォルダー(リソースフォルダーのサブフォルダー)内の単一ファイル.xmlでxmlプロパティを定義する必要があります。

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentDescription="@string/no_descr"
    android:src="@drawable/dummy"/>

次に、属性セットの取得は2行のコードほど簡単ではありません。Scalaで書かれた次の関数が必要です。申し訳ありませんが、私はScalaの人ですが、Javaに固執している場合は、簡単に変換できます。

def getAttributeSetFromXml(xmlId: Int, tagName: String, resources: Resources): AttributeSet = {
/**
 * The good thing for being an internal function is that we don't need to pass tagName as a ref
 */
def getAttributeSet(xmlPullParser: XmlPullParser /*, tagName: String*/): AttributeSet = {
  val state = xmlPullParser.next();
  if (state == XmlPullParser.START_TAG &&
    (xmlPullParser.getName contains tagName)) {
    Xml.asAttributeSet(xmlPullParser);
  }
  else {
    if (state == XmlPullParser.END_DOCUMENT) null;
    else getAttributeSet(xmlPullParser /*, tagName*/);
  }
}

getAttributeSet(resources.getXml(xmlId) /*, tagName*/);
}
于 2012-12-12T17:54:28.670 に答える