Android 类和组件

Android 类和组件,android,Android,我有一个活动,在布局中我使用自己的组件。 这是我自己的组件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <View

我有一个活动,在布局中我使用自己的组件。 这是我自己的组件:

<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>
在我的活动中,我创建此组件的对象,并通过以下方式使用此方法:

        iconsComp = new IconsComponent(getApplicationContext());
        iconsComp.checkActivity(ActivitiesNames.TEST_EXTRA);
但我没有看到任何变化。如何从组件中的活动更改图标?
第二个问题:如何将活动中的一些数据发送到此组件?

您所做的更改是在代码中创建的新实例上进行的,这不会反映到您的布局中。您需要为视图提供一个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 = (IconsComponent) findViewById(R.id.my_view);
<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);