[英]Custom theme interferes with snackbar background color
試用新的設計支持庫,我添加了一個小吃吧; 但與其主背景不同,文本區域未使用默認值#323232
着色。 相反,它看起來像這樣 。 它似乎從我的styles.xml
中的自定義主題中定義的android:background
值中獲取顏色,如下所示:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:background">#4f4f5e</item>
...
</style>
如果我試着用它強行着色
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.YELLOW);
它只影響主背景, 像這樣 ,文本背景仍然被自定義主題着色。 有沒有辦法保持我的自定義主題,並有一個標准的小吃吧? 謝謝!
要更改Snackbar的背景顏色,您可以從代碼中執行以下操作:
Snackbar snack = Snackbar.make(...);
ViewGroup group = (ViewGroup) snack.getView();
group.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
snack.show();
您可以使用Snackbar的默認顏色代替紅色:#323232
.setBackgroundColor
允許您更改snackbar的背景顏色
msnackBar.setBackgroundColor(Color.parseColor("#009688"));
要么
msnackBar.setBackgroundColor(getResources().getColor(R.color.BLUE)););
這是使用設計支持庫使用snackbar的完整教程。
小吃店包含一個TextView,所以你需要改變兩者的背景顏色,就像你已經做的那樣改變小吃吧,然后像這樣改變TextView:
View snackbarView = snackbar.getView();
TextView textView = (TextView)snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setBackgroundColor(Color.YELLOW);
這是一個完整的示例:
Snackbar snack = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null);
ViewGroup group = (ViewGroup) snack.getView();
group.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.blue));
snack.show();
將MainActivity.this替換為您當前的活動或getAppContext()
您可以簡單地創建自己的Snackbar類並模擬Snackbar的make方法。 這樣做,你只需要使用這個類而不是android的snackbar小部件。
Snackbar.class
import android.graphics.Color;
import android.support.annotation.IntDef;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class Snackbar {
/** Snackbar's lengths **/
public static final int LENGTH_SHORT = android.support.design.widget.Snackbar.LENGTH_SHORT;
public static final int LENGTH_LONG = android.support.design.widget.Snackbar.LENGTH_LONG;
public static final int LENGTH_INDEFINITE = android.support.design.widget.Snackbar.LENGTH_INDEFINITE;
@NonNull
public static android.support.design.widget.Snackbar make(@NonNull View view, @NonNull CharSequence text,
@Duration int duration) {
android.support.design.widget.Snackbar snackbar = android.support.design.widget.Snackbar.make(view, text, duration);
// TODO: This is where you have to customize your snackbar
snackbar.getView().setBackgroundColor(Color.RED);
return snackbar;
}
@NonNull
public static android.support.design.widget.Snackbar make(@NonNull View view, @StringRes int resId, @Duration int duration) {
return make(view, view.getResources().getText(resId), duration);
}
// Optional
@IntDef({LENGTH_INDEFINITE, LENGTH_SHORT, LENGTH_LONG})
@IntRange(from = 1)
@Retention(RetentionPolicy.SOURCE)
public @interface Duration {}
}
使用:
// WARNING: Make sure you're using your snackbar's package
import com.mypackage.custom_views.Snackbar;
public class MyActivity extends Activity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Snackbar.make(view, R.string.my_msg, Snackbar.LENGTH_LONG).show();
}
}
希望這可以幫助!
在樣式屬性android:background
中設置時會發生此效果。
刪除它當然會影響應用程序中的所有布局,但小吃欄將被修復。
您可以使用此庫: https : //github.com/SandroMachado/restaurant
new Restaurant(MainActivity.this, "Snackbar with custom background and text color", Snackbar.LENGTH_LONG)
.setBackgroundColor(Color.GRAY)
.show();
免責聲明:我制作了圖書館。
這就是我如何使用自定義零食吧
Snackbar snackbar_network = Snackbar.make(rLayout, "Your Message", Snackbar.LENGTH_SHORT)
.setAction("EXIT", new View.OnClickListener() {
@Override
public void onClick(final View v) {
finish();
}
});
動作文字顏色
snackbar_network.setActionTextColor(Color.RED);
動作消息文本顏色
final View sbView = snackbar_network.getView();
final TextView tv = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.YELLOW);
設置小吃背景
sbView.setBackgroundColor(ContextCompat.getColor(MapsActivity.this, R.color.black));
snackbar_network.show();
以這種方式為我工作:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
Snackbar snackbar = Snackbar.make(lineatLayout, "TEXT", Snackbar.LENGTH_LONG);
ViewGroup group = (ViewGroup) snackbar.getView();
group.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.yourColor));
TextView textView = (TextView) group.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(ContextCompat.getColor(this, R.color.yor collor));
snackbar.show();
我也面臨類似的問題,不幸的是沒有解決方案適合我。因此我編寫自己的解決方案,我也為父視圖設置背景顏色。
TextView snackbarTextView = snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
snackbarTextView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
ViewParent parentView = snackbarTextView.getParent();
if (parentView instanceof View) {
((View) parentView).setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
}
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
snackbar.show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.