我正在尝试在SnackBar
的{{1}}下面显示ToolBar
,
我试过这段代码:
MainActivity
但 mToolbar = (Toolbar) findViewById(R.id.toolbar);
Snackbar snack = Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snack.show();
显示在顶部,但未显示在SnackBar
之下。
我想要类似于Toolbar
应用程序的内容:
主要活动:
Hangout
我正在使用TSnackbar,SnackBar显示在工具栏上
<android.support.design.widget.CoordinatorLayout 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:id="@+id/Coordinator"
xmlns:fab="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:layout_width="75dp"
android:layout_height="28dp"
android:src="@drawable/athena"/>
</android.support.v7.widget.Toolbar>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar5"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
<com.melnykov.fab.FloatingActionButton
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
fab:fab_colorNormal="@color/colorPrimary"
android:src="@drawable/ic_action_name9" />
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:0)
使用PopupWindow使用handler作为
if(!topPopupWindow.isShowing()){
topPopupWindow.showAsDropDown(toolbar);
}
handler.removeMessages(0);
handler.sendEmptyMessageDelayed(0,2500);
答案 1 :(得分:0)
在小吃店顶部设置保证金
Snackbar snack = Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
// calculate actionbar height
TypedValue tv = new TypedValue();
int actionBarHeight=0;
if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
// set margin
params.setMargins(0, actionBarHeight, 0, 0);
view.setLayoutParams(params);
snack.show();
答案 2 :(得分:0)
这对我有用, 设置零食栏的上边距,考虑工具栏尺寸:
View view = snack.getView();
CoordinatorLayout.LayoutParams params2 =(CoordinatorLayout.LayoutParams)view.getLayoutParams();
params2.setMargins(0,50,0,0);
答案 3 :(得分:0)
我在下面给出了我在我的应用中使用的代码以及适用于各种设备的代码
Complie : compile 'com.androidadvance:topsnackbar:1.1.1'
定义视图:
private CoordinatorLayout CSnakbarLayout;
CSnakbarLayout = (CoordinatorLayout) rootView.findViewById(R.id.snackbar_event_mesg);
XML:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/snackbar_event_mesg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
在活动/片段中的代码下面,您想从顶部实现Snackbar:
/**
* Method to display message for on going Event
*/
private void displaySnackbar() {
int duration = 4000;
final TSnackbar snackbar = TSnackbar.make(CSnakbarLayout, getString(R.string.on_going_event_mesg), TSnackbar.LENGTH_INDEFINITE);
snackbar.setActionTextColor(Color.WHITE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
snackbar.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
}, duration);
snackbar.setAction("Ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
snackbar.dismiss();
}
});
View snackbarView = snackbar.getView();
ViewGroup.LayoutParams params = snackbarView.getLayoutParams();
params.height = 300;
snackbarView.setLayoutParams(params);
snackbarView.setBackgroundColor(Color.BLACK);
TextView textView = (TextView) snackbarView.findViewById(com.androidadvance.topsnackbar.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
textView.setMaxLines(5);
snackbar.show();
}
谢谢:)
答案 4 :(得分:0)
在Kotlin中找到解决方案,请尝试:)
val mainLayout: ConstraintLayout = root.findViewById(R.id.main_layout)
val button: Button = root.findViewById(R.id.button)
button.setOnClickListener {
val snackBar = TSnackbar.make(mainLayout, "Snacking with VectorDrawable", TSnackbar.LENGTH_INDEFINITE)
snackBar.setActionTextColor(Color.WHITE)
snackBar.setIconLeft(R.drawable.ic_announcement_black_24dp, 24f)
val snackBarView = snackBar.view
val tv = TypedValue()
if (activity!!.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
val actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
val params = view!!.getLayoutParams() as FrameLayout.LayoutParams
params.setMargins(0, actionBarHeight, 0, 0)
params.height = 70
snackBarView.setLayoutParams(params)
}
snackBarView.setBackgroundColor(Color.parseColor("#CC00CC"))
val textView = snackBarView.findViewById(com.androidadvance.topsnackbar.R.id.snackbar_text) as TextView
textView.setTextColor(Color.YELLOW)
snackBar.setIconPadding(10)
snackBar.show()
}
答案 5 :(得分:0)
由于@Shijil解决方案,我设法在工具栏下方显示MinimalKSnack
。
MinimalKSnack minimalKSnack = new MinimalKSnack(MainActivity.this);
minimalKSnack.setMessage(getResources().getString(R.string.notification))
.setStyle(MinimalKSnackStyle.STYLE_SUCCESS)
.setBackgroundColor(R.color.metallic_blue)
.setAnimation(Fade.In.getAnimation(), Fade.Out.getAnimation())
.setDuration(2000);
View view = minimalKSnack.getMinimalSnackView();
LinearLayout.LayoutParams params =(LinearLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.CENTER_VERTICAL | Gravity.TOP;
TypedValue typedValue = new TypedValue();
int actionBarHeight =0;
if(getTheme().resolveAttribute(R.attr.actionBarSize,typedValue,true)){
actionBarHeight = TypedValue.complexToDimensionPixelOffset(typedValue.data,getResources().getDisplayMetrics());
}
params.setMargins(0,actionBarHeight,0,0);
view.setLayoutParams(params);
minimalKSnack.show();
Github库链接 onurkagan / KSnack