2

別のレイアウトで子レイアウトを複数回再利用するアプリを開発しています。
私の子レイアウトにはボタンが1つしかありません。
私の質問は..子レイアウトのビューに別の方法でアクセスするにはどうすればよいですか。

たとえば、親レイアウトで子レイアウトに3回アクセスします。次に、実行時に親レイアウトに 3 つのボタンを表示します。これらのボタンからさまざまなアクティビティを呼び出したいです。

 My child layout


<RelativeLayout
    android:id="@+id/mylayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_margin="30dp"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/chemistry" />

</RelativeLayout>

My Parent Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg" >

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="fill_parent"
    android:layout_height="470dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
          android:id="@+id/lol"
        android:orientation="horizontal" >



    </LinearLayout>
</HorizontalScrollView>

  Java Code-- 
LinearLayout rl= (LinearLayout)findViewById(R.id.lol);
    LayoutInflater inflater = (LayoutInflater)           this.getSystemService(LAYOUT_INFLATER_SERVICE);

    ArrayList<String> as= getIntent().getExtras().getStringArrayList("dis");
    Object[] mstring= as.toArray();
int x=  mstring.length;
for (int i=0;i<x; i++ ){

    View childLayout = inflater.inflate(R.layout.despage,
            (ViewGroup) findViewById(R.layout.despage));
               rl.addView(childLayout);
4

2 に答える 2

3

このように各インクルードにIDを追加できます

<include android:id=”@+id/child1”
         android:layout_width=”match_parent”
         android:layout_height=”match_parent”
         layout=”@layout/child”/>
<include android:id=”@+id/child2”
         android:layout_width=”match_parent”
         android:layout_height=”match_parent”
         layout=”@layout/child”/>
<include android:id=”@+id/child3”
         android:layout_width=”match_parent”
         android:layout_height=”match_parent”
         layout=”@layout/child”/>

次に、Activity から、次の方法で各子とそのコンテンツを取得できます。

View child1 = findViewById(R.id.child1);
Button button1 = child1.findViewById(R.id.someButtonInsideChildLayout);
View child2 = findViewById(R.id.child2);
Button button2 = child2.findViewById(R.id.someButtonInsideChildLayout);
..

おそらく、スタイルを使用してより良い方法で問題を解決できます (たとえば、values/styles.xml の 1 つのスタイルをメイン レイアウト内の 3 つのボタンに適用します)。

編集(質問に添付されたコードの後)forループ内の各ボタンにアクセスできます

Button button = childLayout.findViewById(R.id.bt1);
于 2013-09-04T09:54:15.367 に答える
0

これがあなたがやりたいことだと思います:

親.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/child" 
    android:text="Button 1">
</Button>

<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/child" 
    android:text="Button 2"
    >
</Button>

<Button
    android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/child"
    android:text="Button 3" >
</Button>

</LinearLayout>

child.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Clicked on button 1",Toast.LENGTH_SHORT).show();
    }
});


findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {

    @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Clicked on    button2",Toast.LENGTH_SHORT).show();
    }
});
于 2013-09-04T10:29:10.590 に答える