1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.support.design.widget; 17 18 import android.content.Context; 19 import android.util.AttributeSet; 20 import android.widget.RelativeLayout; 21 22 import com.example.android.support.design.R; 23 24 /** 25 * Layout for the custom snackbar that shows two separate text views and two images 26 * in the main content area. 27 */ 28 public class CustomSnackbarMainContent extends RelativeLayout { 29 private final int mMaxWidth; 30 CustomSnackbarMainContent(Context context)31 public CustomSnackbarMainContent(Context context) { 32 this(context, null); 33 } 34 CustomSnackbarMainContent(Context context, AttributeSet attrs)35 public CustomSnackbarMainContent(Context context, AttributeSet attrs) { 36 this(context, attrs, 0); 37 } 38 CustomSnackbarMainContent(Context context, AttributeSet attrs, int defStyleAttr)39 public CustomSnackbarMainContent(Context context, AttributeSet attrs, 40 int defStyleAttr) { 41 super(context, attrs, defStyleAttr); 42 43 mMaxWidth = context.getResources().getDimensionPixelSize(R.dimen.custom_snackbar_max_width); 44 } 45 46 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)47 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 48 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 49 50 if ((mMaxWidth > 0) && (getMeasuredWidth() > mMaxWidth)) { 51 super.onMeasure(MeasureSpec.makeMeasureSpec(mMaxWidth, MeasureSpec.EXACTLY), 52 heightMeasureSpec); 53 } 54 } 55 } 56