显示后布局不会隐藏

时间:2014-07-04 21:08:56

标签: android

我正在为Android编写一个需要下拉布局的应用。通过这个我的意思是我想要一个全新的布局出现并在点击一个按钮时消失。我做了一些研究,唯一可行的选择似乎是微调器。但是,微调器不足以满足我想要包含在下拉菜单中的内容(对于上下文,此下拉列表中将有多个滑块,按钮和文本字段,因此需要全尺寸。)

所以我尝试使用XML中的VISIBILITY标记和Java中的setVisible函数来使其工作。当用户单击该按钮时,布局确实会出现,但是当我想通过再次单击该按钮来隐藏菜单时,布局仍然可见,当我希望它“消失”时。

现在进入代码。 首先,显示所有这些的主XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbcde3"
android:orientation="vertical" >

<GridLayout
    android:id="@+id/gridLayout1"
    android:layout_width="fill_parent"
    android:layout_height="35dip"
    android:background="#e3e3e3"
    android:columnCount="2"
    android:gravity="right"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="108dp"
        android:layout_height="match_parent"
        android:layout_column="1"
        android:layout_gravity="right|top"
        android:layout_row="0"
        android:background="#3fa9f5"
        android:fontFamily="helvetica"
        android:text="@string/settings_button"
        android:textColor="#ffffff"
        android:textSize="14sp"
        android:textStyle="bold" />

</GridLayout>

<Space
    android:layout_width="match_parent"
    android:layout_height="12sp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/gridLayout1"
    android:layout_marginTop="24dp"
    android:text="@string/rys"
    android:textColor="#888888"
    android:textSize="19sp" />

<RatingBar
    android:id="@+id/ratingBar1"
    style="@style/circleRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="47dp"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:numStars="5"
    android:rating="3.0"
    android:stepSize="1.0" />

<ImageView
    android:id="@+id/dropDownButton"
    android:layout_width="48dip"
    android:layout_height="48dip"
    android:onClick="dropDown"
    android:layout_alignBottom="@+id/ratingBar1"
    android:layout_toRightOf="@+id/ratingBar1"
    android:src="@drawable/ddb" />

<RelativeLayout
    android:id="@+id/dropDownLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/textView2"
    android:visibility="gone" >

    <TextView 
        android:id="@+id/testTV"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Testing dropdown"
        />
</RelativeLayout>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ratingBar1"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="16dp"
    android:text="@string/tipTitle"
    android:textColor="#888888"
    android:textSize="19sp" />

</RelativeLayout>

这是主要的java文件:

package com.example.tipquiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnRatingBarChangeListener {

// Testing Stuff to show the rating value, will need to use later for maths
RatingBar rb;
TextView tv;
// The Image used as the DropDown button, Rotate code below
ImageView dropDownButton;

RelativeLayout dropDownLayout;
Boolean hasRotated = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    dropDownLayout = (RelativeLayout) findViewById(R.id.dropDownLayout);
    dropDownButton = (ImageView) findViewById(R.id.dropDownButton);
    // tv = (TextView) findViewById(R.id.rating);
    // ((RatingBar) findViewById(R.id.ratingBar1))
    // .setOnRatingBarChangeListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
//Visibilities: 0 = Visible, 1 = Invisible, 2 = Gone
public void dropDown(View view){
    if(hasRotated == false){
        dropDownButton.setRotation(90);
        dropDownLayout.setVisibility(0);
        hasRotated = true;
    }else if(hasRotated == true){
        dropDownButton.setRotation(0);
        dropDownLayout.setVisibility(2);
        hasRotated = false;
    }
}

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
        boolean fromTouch) {
    // final int numStars = ratingBar.getNumStars();
    tv.setText(rating + "/5.0");
}
// http://www.compiletimeerror.com/2013/08/android-rating-bar-example.html#.U7SZ5fldXm4
// http://custom-android-dn.blogspot.ca/2013/01/how-to-use-and-custom-ratingbar-in.html
}

如果有人可以提供帮助,我们将不胜感激。我确定这是一个非常愚蠢的错误,但我受到了阻碍。如果有更好的方式来实现我想做的事情,请分享。

3 个答案:

答案 0 :(得分:1)

你可以使用Fragment,你只需要在你的xml中使用ID定义布局,然后使用Fragment transaction

    transactionPopUp = getChildFragmentManager().beginTransaction();
    // Fragment that hold your items 
    addItemViewFragment = new AddItemViewFragment();
    transactionPopUp.setCustomAnimations(R.anim.abc_slide_in_bottom,
                R.anim.abc_slide_out_top);
    // R.id.pop_up_layout Layout in your xml
    transactionPopUp.add(R.id.pop_up_layout, addItemViewFragment);
    transactionPopUp.commit();

答案 1 :(得分:1)

不确定问题是什么,但我认为你必须使用

.setVisibility(View.VISIBLE)

View.INVISIBLE

View.GONE

android references

答案 2 :(得分:0)

您使用过:

//Visibilities: 0 = Visible, 1 = Invisible, 2 = Gone
...
dropDownLayout.setVisibility(2);

可见性的整数值为:

可见= 0 隐形= 4 走了= 8。

所以你应该用来隐藏视图:

dropDownLayout.setVisibility(8);

甚至更好,因为它对其他人更具可读性:

dropDownLayout.setVisibility(View.GONE);