简体   繁体   中英

How to set the heightof a snackbar?

I produce the following snackbar with the following code:

Snackbar: Snackbar

Code:

View parentLayout = findViewById(android.R.id.content);
        Snackbar snackbar = Snackbar.make(parentLayout, "Wähle mindestens eine Kategorie aus, um fortzufahren", Snackbar.LENGTH_LONG)
                .setAction("CLOSE", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                })
                .setActionTextColor(getResources().getColor(android.R.color.holo_purple ));
        snackbar.show();

As you see, the snackbar is pretty big and I actually only want it to be displayed in the area of the red box. Do you know how to adjust that?

It must bug around with the other Widgets of the Layout . Try to make only one Button and add Widgets step by step. Then you will find out.

I improved your Code a bit and used one Button and the Snackbar like this:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button_snackbar);
        button.setOnClickListener(view -> {
            View parentLayout = findViewById(android.R.id.content);
            Snackbar snackbar = Snackbar.make(parentLayout, "Wähle mindestens eine Kategorie aus, um fortzufahren", Snackbar.LENGTH_LONG)
                    .setAction("CLOSE", view1 -> {
                    })
                    .setActionTextColor(getResources().getColor(android.R.color.holo_purple));
            snackbar.show();
        });
    }
}

PROVE:

小吃店

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM