0

レイアウトで独自のコンポーネントを使用するアクティビティがあります。これは自分のコンポーネントの xml です。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >
    <View 
        android:layout_height="1dp"
        android:layout_width="match_parent"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:background="@color/patient_view_border"/>
    <LinearLayout 
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="8">
    <ImageView
        android:id="@+id/icons_component"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:src="@drawable/_andr" 
        android:layout_weight="1"/>


</LinearLayout>
</LinearLayout>

このコードを追加してアクティビティをレイアウトし、このコンポーネントを追加してアクティビティをレイアウトします。

<test.test.IconsComponent
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">

今、extend linearlayout を使用したコンポーネント クラスには、この画像の画像を変更するメソッドがあります。

public void checkActivity(ActivitiesNames name){
    if(name == ActivitiesNames.TEST_EXTRA){
        testIcon.setImageDrawable(getResources().getDrawable(R.drawable.legendaw_andr));
    }
}

私のアクティビティでは、このコンポーネントのオブジェクトを作成し、次の方法でこのメソッドを使用します。

        iconsComp = new IconsComponent(getApplicationContext());
        iconsComp.checkActivity(ActivitiesNames.TEST_EXTRA);

しかし、私は何の変化も見ません。コンポーネントのアクティビティからアイコンを変更するにはどうすればよいですか? 2 番目の質問: アクティビティからこのコンポーネントにデータを送信するにはどうすればよいですか?

4

1 に答える 1

1

行った変更は、コードで作成した新しいインスタンスにあり、レイアウトには反映されません。ビューに ID を与える必要があります。

<test.test.IconsComponent
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">

次に、新しいインスタンスを作成する代わりに:

iconsComp = new IconsComponent(getApplicationContext());

すでに膨らんだものを参照する必要があります。

iconsComp = (IconsComponent) findViewById(R.id.my_view);
于 2013-07-02T10:15:30.563 に答える