简体   繁体   中英

Why ImageView is NOT VISIBLE in Layout?

I want to make an image appears as VISIBLE in the layout by programming it.

This is the XML file where I declare the image as GONE for then make it VISIBLE from code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/map"
    tools:context="dis2.widget.MainActivity">

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:id="@+id/contactsScrollView"
        android:fillViewport="false"
        android:visibility="visible"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="26dp">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone">

            <ImageView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:src="@drawable/linkg"
                android:id="@+id/linkgID" 
                android:visibility="gone"/>

        </LinearLayout>
    </HorizontalScrollView>

</RelativeLayout>

And here is the MainActivity Java file :

private greenBeeper g;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView b = (ImageView) findViewById(R.id.linkgID);
    b.setVisibility(View.VISIBLE);
}

You have made your LinearLayout as well as ImageView gone in xml. While in java you are only making your image visible and your imageView parent is still in gone state. That is the problem in your code. Give some id to your linear layout too and make it visible when you want your imageview to be visible

Your LinearLayout is stated as gone .

    <LinearLayout
        android:id="@+id/imageParent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="gone">

        <ImageView
            android:id="@+id/linkgID"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:src="@drawable/linkg"
            android:visibility="gone"/>

    </LinearLayout>

Try removing the android:visibility="gone" from the LinearLayout , and use the rest of you Java code.

Try this, In your xml change visibility of LinearLayout "visible" instead of "gone" and you can see imageview in your layout.

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:src="@drawable/linkg"
            android:id="@+id/linkgID" 
            android:visibility="gone"/>

    </LinearLayout>

I hope you solve your problem with this..!!

Enable both linear layout and image view visibility.

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible">
        <ImageView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:src="@drawable/board_out_icon"
            android:id="@+id/linkgID"
            android:visibility="visible"/>

    </LinearLayout>
private greenBeeper g;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    ImageView b = (ImageView) findViewById(R.id.linkgID);
    b.setVisibility(View.VISIBLE);
    layout.setVisibility(View.VISIBLE);
}

give id to LinearLayout too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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