[英]Android setVisibility(View.Visible) not working on a layout
I have a layout that I am trying to make visible and it is currently not working.我有一个布局,我正在尝试使其可见,但它目前无法正常工作。 The layout I want to make visible has the id "goal_reminder" below.
我要使其可见的布局在下面具有 ID“goal_reminder”。 The visibility is set to "GONE" in the xml.
xml 中的可见性设置为“GONE”。
Here is the xml这是xml
<?xml version="1.0" encoding="utf-8"?>
<com.View.pages.ActivityPage
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/activity_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/activitylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e7e6ee"
android:clipToPadding="false"
android:divider="@null"
android:paddingBottom="80dp" />
</android.support.v4.widget.SwipeRefreshLayout>
<RelativeLayout
android:id="@+id/goal_reminder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent_color"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:layout_width="match_parent"
android:layout_height="145dp"
android:src="@drawable/sunsetforgoal" />
<ImageView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/goal_reminder_title"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="35dp"
android:src="@drawable/logo" />
<TextView
android:id="@+id/goal_reminder_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="No goal in progress"
android:textColor="@color/text_color"
android:textSize="26sp" />
<TextView
android:id="@+id/goal_reminder_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/goal_reminder_title"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="Create a new goal in your \nprofile."
android:textColor="@color/text_color"
android:textSize="18sp" />
</RelativeLayout>
<TextView
android:id="@+id/playground_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:autoLink="all"
android:gravity="center_horizontal"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="@string/welcome_string"
android:textSize="18sp"
android:visibility="gone" />
</com.View.pages.ActivityPage>
Here is the onFinishInflate(I am using the Screenplay/Flow libraries so that is taking place of onCreate).这是 onFinishInflate(我正在使用 Screenplay/Flow 库,所以它正在代替 onCreate)。 As you can see I am trying to set goalReminderLayout.setVisibility(View.Visible) and it's not actually making it visible.
如您所见,我正在尝试设置 goalReminderLayout.setVisibility(View.Visible) 并且实际上并没有使其可见。 I have tested this same line of code outside of the if statements and it works just fine.
我已经在 if 语句之外测试了同一行代码,它工作得很好。 I have also tested to make sure it's reaching that line of code in the if statements and that part is working fine, the lastTriggerDate is being saved properly in Parse.
我还进行了测试以确保它到达 if 语句中的那行代码并且该部分工作正常,lastTriggerDate 正确保存在 Parse 中。 I am lost on why it's working fine outside of the if statements in the onFinishInflate.
我不知道为什么它在 onFinishInflate 中的 if 语句之外工作正常。 I also tested with a Log.d("TestVisiblity", goalReminderLayout.getVisibility());
我还用 Log.d("TestVisiblity", goalReminderLayout.getVisibility()); 进行了测试which returns a 0(Visible) so it seems like its visible but its not actually showing up on my screen.
它返回一个 0(Visible),所以它看起来像是可见的,但实际上并没有出现在我的屏幕上。
Date lastTriggerDate = new Date();
Boolean noDateInParse = false;
if (currentUser.getLastTriggerDate() != null) {
lastTriggerDate = currentUser.getLastTriggerDate();
} else {
noDateInParse = true;
}
Boolean inToday = DateUtils.isToday(lastTriggerDate.getTime());
if (!inToday) {
currentUser.setLastTriggerDate(currentTime);
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
goalReminderLayout.setVisibility(View.VISIBLE);
fireGoalReminderAlert();
} else {
e.printStackTrace();
}
}
});
} else if (noDateInParse) {
currentUser.setLastTriggerDate(currentTime);
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
goalReminderLayout.setVisibility(View.VISIBLE);
fireGoalReminderAlert();
} else {
e.printStackTrace();
}
}
});
}
Here is the code for fireGoalReminderAlert();这是 fireGoalReminderAlert() 的代码; It's just set to set the visibility back to gone after 10 seconds.
它只是将可见性设置为在 10 秒后消失。 I commented this line out when testing and still had no luck so I don't think this is causing the problem.
我在测试时评论了这条线,但仍然没有运气,所以我认为这不是导致问题的原因。
public void fireGoalReminderAlert() {
Runnable mRunnable;
Handler mHandler = new Handler();
mRunnable = new Runnable() {
@Override
public void run() {
goalReminderLayout.setVisibility(View.GONE);
}
};
mHandler.postDelayed(mRunnable, 10 * 1000);
}
When you set a layout to visible from gone it's always a good idea to invalidate view to allow a redraw on the layout.当您将布局设置为从消失时可见时,使视图无效以允许在布局上重绘总是一个好主意。
goalReminderLayout.setVisibility(View.VISIBLE); targetReminderLayout.setVisibility(View.VISIBLE);
goalReminderLayout.invalidate();目标提醒布局.invalidate();
look at the android documentation for more info查看 android 文档以获取更多信息
http://developer.android.com/reference/android/view/View.html http://developer.android.com/reference/android/view/View.html
Try this尝试这个
Handler(Looper.getMainLooper()).post {
goalReminderLayout.setVisibility(View.VISIBLE);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.