2011-10-29 2 views
0

저는 초보자이므로 도움을 주시면 감사하겠습니다. StackOverflow에서 많은 게시물을 읽었으며 Google에서 의심의 여지가 있었지만 좋은 대답을 찾기가 어려웠습니다. 여기 FrameLayout 및 TextView를 사용하여 사용자 정의 ImageButton 클래스 만들기

내가 할 노력하고 무엇 : 너희들은 내가 필요의 XML 버전을 볼 수 있습니다

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1">  
    <ImageButton 
     android:background="@layout/roundcorners" 
     android:id="@+id/hug" 
     android:scaleType="centerInside" 
     android:cropToPadding="false" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:paddingBottom="10dip" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:src="@drawable/hug"> 
    </ImageButton> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|center" 
     android:textColor="#000000" 
     android:textSize="15dip" 
     android:textStyle="bold" 
     android:paddingBottom="5dip" 
     android:clickable="true" 
     android:text="Hug"> 
    </TextView> 
</FrameLayout> 

위.

요점은 ... 런타임에 이러한 FrameLayouts가 많이 있습니다. 버튼을 채울 수있는 모든 정보는 데이터베이스에서 가져옵니다.

그래서 데이터베이스에서 모든 레지스터를 통해 루프를 사용할 수 있고 새로운 FrameLayout 클래스를 인스턴스화 할 수있는 Java 클래스를 만들어야합니다 (이 클래스는 XML에서 볼 수있는 것처럼 ImageButton과 TextView가 있어야합니다) 다음과 같이 매개 변수를 전달하십시오.

for (int i = 0; i < mArray.length; i++) { 
     button = new MyNewImageButton(name, src, text); 
} 

위의 내용은 단순화하는 것입니다. 나는이 클래스의 인스턴스를 만들 때 데이터베이스에서 매개 변수를 전달한다는 것을 의미합니다. 물론 만들어진 모든 단일 버튼이 레이아웃에 추가됩니다.

내 질문은 : XML을 사용하여이를 수행하는 방법을 알고 있지만 실제로이 작업을 수행하는 데 어려움을 겪고 있습니다.

의견이 있으십니까? 어떤 도움을 주셔서 감사합니다.

피씨 : 죄송합니다. 내 영어로 실수하면 죄송합니다. 나는 브라질 사람입니다. 언젠가 제 영어 실력도 완벽해질 것입니다! 이 질문에 이미 답변 한 경우 죄송합니다.


미안하지만 다른 질문을하는 것이 좋습니다. 나는 주석을 사용하려고 시도했지만 문자의 수에 제한이있어서 정말 미안하다.

안녕 얘들 아 @blessenm. 음 ... 나는 인플레이터를 사용하려고하고 다음 코드를 내놓았다 :

super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    // ******************************************* 
    setContentView(R.layout.main);  


    //this is my main screen 
    //it's a linearlayout vertical orientation 
    ViewGroup parent = (ViewGroup) findViewById(R.id.tela_principal); 

    //these two new LinearLayouts will be one above the other, 
    //just like two lines 
    LinearLayout l1 = new LinearLayout(this); 
    LinearLayout l2 = new LinearLayout(this); 

    //inside of each linearlayout I set the orientation to horizontal 
    //so, everytime a picture is inflated from xml, it will fill in one 
    //linearlayout 
    l1.setOrientation(LinearLayout.HORIZONTAL); 
    l2.setOrientation(LinearLayout.HORIZONTAL); 

    //setting linearlayout parameters, so they fill the whole screen 
    l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 


    //the first two inflated xml imagebuttons I add to LinearView1 
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao, 
      l1, true); 
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao, 
      l1, true); 

    //the next two inflated xml imagebuttons I add to LinearView2 
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao, 
      l2, true); 
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao, 
      l2, true); 

    //after the above, we should have a grid 2X2 


    //after linearlayouts are filled, I add them to the main screen 
    parent.addView(l1, new LinearLayout.LayoutParams(0, 0, 1)); 
    parent.addView(l2, new LinearLayout.LayoutParams(0, 0, 1)); 

을하지만이 작동하지 않습니다. 오류 로그에 다음 메시지가 나타납니다.

"처리되지 않은 이벤트 루프 예외".

내가 뭘 잘못하고 있는지에 대한 아이디어가 있으십니까?

감사합니다.

답변

1

xml에서보기를 만들고 레이아웃에 추가하려는 경우. LayoutInflater를 사용하면됩니다. 당신이 클래스는 프레임 레이아웃이나 뷰를 확장 만들려고하는 경우 활동 내부

FrameLayout frame = (FrameLayout)getLayoutInfalter.inflate(
     R.id.YOUR_VIEW_XML,null); 
layout.addView(frame); 

같은 것을 사용합니다. 매개 변수를 사용하고 필요한 값을 할당하는 생성자를 만듭니다.

편집 : 당신이 그 요소의 id를 설정 한 경우

TextView text = (TextView)frame.findViewById(R.id.yourtextview); 

에 의해 액세스 할 수 있습니다 또는 당신은 그것은 소리

TextView text = (TextView)frame.getChildAt(0); 
+0

안녕하세요 Blessenm는, 그래, 당신의 제안은 감사를했다. * *, 어떻게 부풀려진 XML 내부의 각 요소를 참조 할 수 있습니까? 내 말은, ImageButton과 TextView에 대한 참조가 필요하므로 데이터베이스의 값으로 설정할 수 있습니다. 어떤 제안이 있으십니까? –

+0

ive가 내 대답을 업데이트했습니다. – blessenm

0

같은 자식 인덱스를 사용할 수 있습니다 ACESS 요소 내부 에 ImageButton이 될 뷰 클래스와 FrameLayout으로 래핑 된 TextView를 만드는 방법을 찾고있는 것처럼 말입니다.

이 경우 자신 만의 View 클래스를 만들 수 있습니다. FrameLayout을 확장하는 View 클래스 일 것입니다. 사용자 지정보기를 만드는 방법에 대한 자세한 내용은이 개발 문서를 참조하십시오. http://developer.android.com/guide/topics/ui/custom-components.html

구체적으로 "복합 제어"섹션 : http://developer.android.com/guide/topics/ui/custom-components.html#compound

관련 문제