初始化自定义视图元素

时间:2019-04-24 11:00:03

标签: android android-custom-view

我最近启动了Android,并且正在与个性化轮播进行对话。

我创建了一个xml,其中包含在旋转木马的每个视图(ImageView和TextView)中都可以看到的两个元素,但是当我创建View对象时,我不知道如何访问每个元素以对其进行初始化。 / p>

XML自定义视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/imageView_carousel_presentacion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/com_facebook_button_icon_blue" />

    <TextView
        android:id="@+id/textView_carouser_presentacion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

对话框类

public class CarouserInicialDialogo {

    private Context contexto;

    private Dialog dialogo;
    private Button btn;

    private CarouselView carousel;
    private int[] imagenes = {R.drawable.default_profile, R.drawable.felicitades, R.drawable.logo_app};

    public CarouserInicialDialogo(final Context contexto) {
        dialogo = new Dialog(contexto);
        dialogo.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogo.setCancelable(true);
        dialogo.setContentView(R.layout.dialogo_carousel_inicial);

        this.contexto = contexto;

        carousel = (CarouselView) dialogo.findViewById(R.id.carousel_view_presenacion);
        carousel.setPageCount(imagenes.length);
        carousel.setViewListener(viewListener);

        dialogo.show();
    }

    ViewListener viewListener = new ViewListener() {
        @Override
        public View setViewForPosition(int position) {
            LayoutInflater inflater = (LayoutInflater) contexto.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            View customView = inflater.inflate(R.layout.custom_view_carouser_presentacion, null);

            //set view attributes here
            //
            //
            //

            return customView;
        }
    };
}

2 个答案:

答案 0 :(得分:0)

您可以访问这样的视图

ImageView imageView = (ImageView) customView.findViewById(R.id.imageView_carousel_presentacion);
TextView textView = (TextView) customView.findViewById(R.id.textview_carousel_presentacion);

添加此代码后,您的类如下所示:

public class CarouserInicialDialogo {

private Context contexto;

private Dialog dialogo;
private Button btn;

private CarouselView carousel;
private int[] imagenes = {R.drawable.default_profile, R.drawable.felicitades, R.drawable.logo_app};

public CarouserInicialDialogo(final Context contexto) {
    dialogo = new Dialog(contexto);
    dialogo.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialogo.setCancelable(true);
    dialogo.setContentView(R.layout.dialogo_carousel_inicial);

    this.contexto = contexto;

    carousel = (CarouselView) dialogo.findViewById(R.id.carousel_view_presenacion);
    carousel.setPageCount(imagenes.length);
    carousel.setViewListener(viewListener);

    dialogo.show();
}

ViewListener viewListener = new ViewListener() {
    @Override
    public View setViewForPosition(int position) {
        LayoutInflater inflater = (LayoutInflater) contexto.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View customView = inflater.inflate(R.layout.custom_view_carouser_presentacion, null);

            ImageView imageView = (ImageView) customView.findViewById(R.id.imageView_carousel_presentacion);
            TextView textView = (TextView) customView.findViewById(R.id.textview_carousel_presentacion);

            //Views are initialized with layout.

        return customView;
    }
};
}

答案 1 :(得分:-1)

尝试一下:

customView.findViewById(R.id.imageView_carousel_presentacion)