2012-09-20 3 views
2

런타임에 Misc widgets for Android platform에서 패널을 만들고 싶습니다.AttributeSet에 대한 XML을 올바르게 작성하는 방법은 무엇입니까?

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

panel_attribute.xml은 무엇이되어야합니까?

패널은 레이아웃 내부에서와 마찬가지로 당신이 유사하게 XML 폴더 (리소스 폴더의 하위 폴더) 내부에 단일 파일 .XML의 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" /> 

답변

1

먼저 같아야합니다 파일.

<?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"/> 

두 번째로 속성 집합을 검색하는 것은 두 줄의 코드만큼 쉽지 않습니다. 스칼라로 작성된 다음 함수가 필요합니다. 미안하지만 저는 스칼라 녀석입니다. 그렇다면 자바로 갇혀 있으면 쉽게 변환 할 수 있습니다!

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*/); 
} 
관련 문제