当SnackBar出现在CoordinatorLayout中时,向上移动视图

时间:2017-03-10 02:41:44

标签: java android android-coordinatorlayout snackbar

我在TextView的底部有一个CoordinatorLayout

但是当我显示SnackBar时,它会覆盖TextView

我知道我必须为Behavior自定义TextView并覆盖layoutDependsOnonDependentViewChanged,但它并没有很好地修复。

如果你知道的话,你可以给我一些建议吗?感谢。

2 个答案:

答案 0 :(得分:35)

TextView是CoordinatorLayout的直接子项,只需添加

$user_Id

属性中的

魔术!

答案 1 :(得分:1)

您需要向import ujson from datetime import datetime from dateutil import tz from csv import writer import time def hms_string(sec_elapsed): h = int(sec_elapsed / (60 * 60)) m = int((sec_elapsed % (60 * 60)) / 60) s = sec_elapsed % 60. return "{}:{:>02}:{:>05.2f}".format(h, m, s) start_time = time.time() with open('G:\Programming Projects\GGS 681\dmv_raw_tweets1.json', 'r') as in_file, \ open('G:\Programming Projects\GGS 681\dmv_tweets1.csv', 'w') as out_file: print >> out_file, 'user_id,timestamp,latitude,longitude' csv = writer(out_file) tweets_count = 0 for line in in_file: tweets_count += 1 tweets = ujson.loads(line) timestamp = [] lats = '' longs = '' for tweet in tweets: tweet = tweets from_zone = tz.gettz('UTC') to_zone = tz.gettz('America/New_York') times = tweet['created_at'] for tweet in tweets: times = tweets['created_at'] utc = datetime.strptime(times, '%a %b %d %H:%M:%S +0000 %Y') utc = utc.replace(tzinfo=from_zone) #comment out to parse to utc est = utc.astimezone(to_zone) #comment out to parse to utc timestamp = est.strftime('%m/%d/%Y %I:%M:%S %p') # use %p to differentiate AM/PM for tweet in tweets: if tweets['geo'] and tweets['geo']['coordinates'][0]: lats, longs = tweets['geo']['coordinates'][:2] else: pass row = ( tweets['user']['id'], timestamp, lats, longs ) values = [(value.encode('utf8') if hasattr(value, 'encode') else value) for value in row] csv.writerow(values) end_time = time.time() print "{} to execute this".format(hms_string(end_time - start_time)) 添加行为并将其嵌入LinearLayout

这是你如何做到的。

<强> MoveUpwardBehavior.class

CoordinatorLayout

<强> CustomLinearLayout.class

import android.os.Build;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.view.View;


public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> {
    private static final boolean SNACKBAR_BEHAVIOR_ENABLED;

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }

    static {
        SNACKBAR_BEHAVIOR_ENABLED = Build.VERSION.SDK_INT >= 11;
    }
}

示例xml-&gt; activity_home

这里 user.example.charu.its2017huree 是我的包名,用你的名字替换它!

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.widget.LinearLayout;

@CoordinatorLayout.DefaultBehavior(MoveUpwardBehavior.class)
public class CustomLinearLayout extends LinearLayout {
    public CustomLinearLayout(Context context) {
        super(context);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

最后在我的名为 HomeActivity

的活动中
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:layout_height="match_parent">
        <user.example.charu.its2017huree.CustomLinearLayout
            android:background="#098"
            android:gravity="bottom"
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello world" />
        </user.example.charu.its2017huree.CustomLinearLayout>

来自此example