2016-04-17 7 views
0

RelativeLayoutにプログラムでアクセスする方法がわからないので、ハンドラ内でrelativeLayout.getChildCount();などのメソッドを呼び出すことができます。変数としてRelativeLayoutにアクセスする方法は?

編集 - 大丈夫、RelativeLayoutのビューの総数を取得したい場合は、どのような関数を呼びますか?フォルダのres /レイアウトで

+0

あなたの質問を明確にしてください –

+0

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

+0

はい、ここの問題は私のXML相対レイアウトで定義されたビューの数を取得したいのですが、どのように私はそれをJavaフォームにリンクするのですか? – user6191359

答えて

0

、私はあなたのレイアウトがlayout.xml

  1. という名前と仮定し、あなたのRelativeLayoutはアンドロイド持っていることを確認します。idセグメント

    <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. アクセスRelativeLayout IDの活動

    でいます
    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

ありがとう、完璧に動作します! – user6191359

+0

答えとして受け入れてください – keronconk

関連する問題