我尝试在活动开始时展示小吃店,但不能工作。
final Snackbar snackbar = Snackbar.make(???, "test", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("dismiss", new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
}
});
snackbar.show();
答案 0 :(得分:1)
完整工作演示
MainActivity
package pk.sohail.gallerytest.activity;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import pk.sohail.gallerytest.R;
public class MainActivity extends AppCompatActivity {
Context context;
private CoordinatorLayout coordinatorLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
coordinatorLayout = (CoordinatorLayout) findViewById(R.id
.coordinatorLayout);
ImageView image = (ImageView) findViewById(R.id.myimage);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});
snackbar.show();
}
});
}
}
<强> activity_main.xml中强>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white">
<ImageView
android:id="@+id/myimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
风格: parent =&#34; Theme.AppCompat.Light&#34;
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<!--<item name="colorPrimary">@color/colorPrimary</item>-->
<!--<item name="colorPrimaryDark">@color/colorPrimaryDark</item>-->
<!--<item name="colorAccent">@color/colorAccent</item>-->
</style>
Manifest:在应用标记中将主题设置为@style/AppTheme
。
<application
...
android:theme="@style/AppTheme"
...
>
</application>
答案 1 :(得分:0)
你可以试试这个
将它放在您的activity_main.xml
中<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:visibility="gone"
android:src="@android:drawable/ic_dialog_email" />
//这个按钮看不见
现在在活动oncreate o方法中编写此代码,以便在应用启动时显示一次
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
fab.performClick();
答案 2 :(得分:0)
我认为你可以使用这个
Snackbar snackbar = Snackbar.make(snackbarCoordinatorLayout, "Snackbar",
Snackbar.LENGTH_LONG);
然后将动作设置为小吃栏,即,当您点击小吃栏时,将执行此操作
snackbar.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SnackBarActivity.this,"snackbar OK clicked",
Toast.LENGTH_LONG).show();
}
});
了解更多信息请点击here