3

我在 relativelayout 中有一个 relativelayout,它都在 include 中。外部 relativelayout 有一个 android:background 是一个图像。
内在有其他形象,但方式相同。
这一切都是看不见的。 有一个按钮负责切换可见性。

当它变得可见时 - 显示内部图像但不显示外部图像。
但是,如果我单击隐藏然后再次单击以显示 - 外部图像也会显示。

这是相关的xml:

<RelativeLayout
    android:id="@+id/display_prefs_dialog_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="invisible" >

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="45dp"
        android:layout_marginRight="50dp"
        layout="@layout/display_prefs_dialog" >
    </include>
</RelativeLayout>

里面包括:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/display_prefs_dialog"
    android:layout_width="327dp"
    android:layout_height="218dp"
    android:background="@drawable/display_win" >

    <RelativeLayout
        android:id="@+id/display_letters_bg"
        android:layout_width="262dp"
        android:layout_height="44dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="38dp"
        android:background="@drawable/display_letter_frame" >

JAVA代码:

final ImageButton btnDisplay = (ImageButton) findViewById(R.id.btnDisplay);         
        final RelativeLayout prefsInnerDialog = (RelativeLayout) findViewById(R.id.display_prefs_dialog);       
        final RelativeLayout prefsDialog = (RelativeLayout) findViewById(R.id.display_prefs_dialog_layout);


btnDisplay.setOnClickListener(new Button.OnClickListener() 
        {
            public void onClick(View v)
            {                               
                int visibility = prefsDialog.getVisibility();
                if (visibility == View.VISIBLE) 
                {
                    prefsDialog.setVisibility(View.INVISIBLE);
                } 
                else 
                {
                    prefsDialog.setVisibility(View.VISIBLE);
                }
            }
        });
4

1 回答 1

0

I have been in the same kind of the requirement today as yours and have done something similar to this, and its working for me. Posting the same sample code for you, that might help you..!

Java File : Main.java

public class Main extends Activity {
    RelativeLayout prefsInnerDialog, prefsDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        prefsInnerDialog = (RelativeLayout) findViewById(R.id.display_prefs_dialog);
        prefsDialog = (RelativeLayout) findViewById(R.id.display_prefs_dialog_layout);
    }
    public void toggleIt(View v) {
        int visibility = prefsDialog.getVisibility();
        if (visibility == View.VISIBLE) {
            prefsDialog.setVisibility(View.INVISIBLE);
        } else {
            prefsDialog.setVisibility(View.VISIBLE);
        }
    }
}

First XML : main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" android:id="@+id/linearLayout1">
    <Button android:layout_width="wrap_content" android:text="Button"
        android:id="@+id/button1" android:layout_height="wrap_content"
        android:onClick="toggleIt"></Button>
    <RelativeLayout android:id="@+id/display_prefs_dialog_layout"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:visibility="invisible">
        <include android:layout_width="fill_parent"
            android:layout_height="fill_parent" layout="@layout/mainsecond">
        </include>
    </RelativeLayout>
</LinearLayout>

Second XML : mainsecond.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/display_prefs_dialog" android:layout_width="320dp"
    android:layout_height="480dp" android:background="@drawable/mainbg">
    <RelativeLayout android:id="@+id/display_letters_bg"
        android:layout_centerInParent="true"
        android:layout_width="250dp" android:layout_height="100dp"
        android:background="@drawable/databg">
    </RelativeLayout>
</RelativeLayout>

Looking at your code , i feel like there is nothing wrong in the java code, you having problem just because the bad RelativeLayout usages.

于 2012-05-19T06:02:47.193 回答