2016-04-17 11 views
0

Ich kann nicht herausfinden, wie auf mein RelativeLayout programmgesteuert zugegriffen wird, damit ich eine Methode wie relativeLayout.getChildCount(); in einem Handler aufrufen konnte.Wie bekomme ich RelativeLayout als Variable?

Bearbeiten - in Ordnung, wenn ich die Gesamtzahl der Ansichten in meinem RelativeLayout abrufen wollte, welche Funktion würde ich aufrufen?

+0

Bitte Stellen Sie Ihre Frage klar –

+0

http://stackoverflow.com/questions/6615723/getting-child-elements-from-linearlayout –

+0

Ja, aber das Problem hier ist, möchte ich die Anzahl der Ansichten in meinem XML-Layout definiert, Wie verknüpfe ich das mit einem Java-Formular? – user6191359

Antwort

0

In Ordner res/Layout, gehe ich davon aus, das Layout namens layout.xml

  1. stellen Sie sicher, Ihre RelativeLayout haben android: id Segment

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
         xmlns:tools="http://schemas.android.com/tools" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:id="@+id/myRelativeLayout" > //the id name can be acsessed in activity 
    
         <TextView 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="@string/hello_world" /> 
    
    </RelativeLayout> 
    
  2. zugreifen, die RelativeLayout id in Aktivität

    public class mainActivity extends Activity { 
        //make variable globally 
        RelativeLayout myRelativeLayout; 
    
    
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         //the layout file 
         setContentView(R.layout.layout); 
    
         //access the RelativeLayout and initialiszed the object 
         myRelativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout); 
         myRelativeLayout.getChildCount(); 
    
        } 
    } 
    
+0

Danke, funktioniert perfekt! – user6191359

+0

Bitte akzeptieren Sie es als Antwort – keronconk

Verwandte Themen