简体   繁体   English

如何在包含的布局中设置android ImageView可见性?

[英]how to set android ImageView visibility inside an included layout?

We are using android API 17 in our application. 我们在我们的应用程序中使用android API 17。 I have defined a layout containing two images vies as below: 我已经定义了一个包含两个图像的布局,如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image_container_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<ImageView
        android:id="@+id/image_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_1_resource"/>

<ImageView
        android:id="@+id/image_2"
        android:layout_marginTop="3dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image_container_layout"
        android:src="@drawable/image_2_resource"/>

This layout is included inside another layout as below: 此布局包含在另一个布局中,如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    style="@style/wizard_content_style"
    tools:context=".ui.Wizard"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    >


<include layout="@layout/image_container_layout"
         android:id="@+id/included_view"
            />

<TextView
        style="@style/wizard_content_text_style_medium"
        android:id="@+id/text_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/included_view"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal"
        android:text="@string/instruction"
        android:layout_marginBottom="15dp"/>

The reason that the layout is included is that we want to reuse it in two more layouts. 包含布局的原因是我们希望在两个以上的布局中重用它。 Now based on some condition I want to hide or show the image views inside image_container_layout . 现在基于某些条件我想隐藏或显示image_container_layout的图像视图。

The java code looks like this: java代码如下所示:

  containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
   image1 = (ImageView) containerLayout.findViewById(R.id.image_1);
   image2 = (ImageView) containerLayout.findViewById(R.id.image_2);
  switch (accuracy) {
    case 1:
        log().i(getClass().getSimpleName(), "case 1 chosen");
        image1.setVisibility(View.VISIBLE);
        image2.setVisibility(View.GONE);
        log().i(getClass().getSimpleName(), "image 1 has been shown");
        break;
    case 2:
        image1.setVisibility(View.GONE);
        image2.setVisibility(View.VISIBLE);
        break;
    case 3:
        image1.setVisibility(View.VISIBLE);
        image2.setVisibility(View.VISIBLE);
        break;
}

I am debugging this code and I am sure the code is running. 我正在调试此代码,我确信代码正在运行。 The log messages are printed in Logcat as well, but nothing happens no change in the images. 日志消息也在Logcat中打印,但没有任何变化,图像也没有变化。 Also, both images are always shown. 此外,始终显示两个图像。 I wonder if there is something that I have to do when working with the included layout? 我想知道在使用包含的布局时是否还有一些事情要做? Thanks for any help in advance. 在此先感谢您的帮助。 Based on answers I got below, seems that inflating a view will create a new object and because of this, changes in the visibility are not shown on the user interface. 基于我在下面得到的答案,似乎膨胀视图将创建一个新对象,因此,可见性的更改不会显示在用户界面上。 Then the question is that if we have a wizard and inside 3 different pages of the wizard I want to have an image and depending on some condition I want to show or hide the image, what is the best solution? 然后问题是,如果我们有一个向导并且在向导的3个不同页面内我想要一个图像,并且根据某些条件我想显示或隐藏图像,最佳解决方案是什么? I mean I want to reuse the code which is responsible for hiding and showing the image regardless which page of wizard is active. 我的意思是我想重用负责隐藏和显示图像的代码,无论向导的哪个页面处于活动状态。

Why are you complexing with so much code. 你为什么要用如此多的代码复杂化 If you include some layout in your xml then you can use those widgets also same as the xml have. 如果你在xml中包含一些布局,那么你可以使用那些与xml相同的小部件。 There is no need to inflate. 没有必要膨胀。

ImageView image_2 = findViewById(R.id.image_2);
image_2.setVisbility(Visible.GONE);

You said at this comment the code not inside activity but wherever it is you inflated a new layout to your view currently displaying by this line: containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null); 你在这个评论中说过代码不在活动内部,但无论你在哪里为你当前显示的行显示了新的布局: containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);

When you try to change visibility of those images actually it works, i think so. 当你试图改变这些图像的可见性时,它实际上是有效的,我想是的。 But if your activity or fragment layout contains image_container_layout maybe you see those images. 但是,如果您的活动或片段布局包含image_container_layout ,您可能会看到这些图像。

And I wonder that what do you do with inflated view containerLayout . 我想知道你如何处理膨胀的视图containerLayout Do you add it to inside of any other view. 您是否将其添加到任何其他视图内部。 If you dont it wont be visible for you. 如果你不对它不明显。

you have to use it like this: 你必须像这样使用它:

View included_view1 = findViewById(R.id.included_view1);
ImageView image_1 = included_view1.findViewById(R.id.image_1);
ImageView image_2 = included_view1.findViewById(R.id.image_2);
image_1.setVisibility(View.VISIBLE);
image_1.setVisibility(View.GONE);
image_2.setVisibility(View.VISIBLE)
image_2.setVisibility(View.GONE)

View included_view2 = findViewById(R.id.included_view2);
ImageView image_11 = included_view2.findViewById(R.id.image_1);
ImageView image_22 = included_view2.findViewById(R.id.image_2);
image_11.setVisibility(View.VISIBLE);
image_11.setVisibility(View.GONE);
image_22.setVisibility(View.VISIBLE)
image_22.setVisibility(View.GONE)

Above code will be helpful in the case of multiple time you want to use same layout. 如果您想要使用相同的布局,上面的代码将会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM