1/* 2 * Copyright (C) 2017 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 17 18package com.android.tv.settings.connectivity.setup; 19 20import android.content.Context; 21import android.content.res.TypedArray; 22import android.util.AttributeSet; 23import android.widget.RelativeLayout; 24import android.widget.TextView; 25 26import com.android.tv.settings.R; 27 28/** 29 * Relative layout implementation that lays out child views based on provided keyline percent( 30 * distance of TitleView baseline from the top). We do the similar thing in 31 * {@link com.google.android.tungsten.setupwraith.ui.GuidanceRelativeLayout} 32 */ 33public class GuidanceRelativeLayout extends RelativeLayout { 34 private float mTitleKeylinePercent; 35 36 public GuidanceRelativeLayout(Context context) { 37 this(context, null); 38 } 39 40 public GuidanceRelativeLayout(Context context, AttributeSet attrs) { 41 this(context, attrs, 0); 42 } 43 44 public GuidanceRelativeLayout(Context context, AttributeSet attrs, int defStyle) { 45 super(context, attrs, defStyle); 46 mTitleKeylinePercent = getKeyLinePercent(context); 47 } 48 49 private static float getKeyLinePercent(Context context) { 50 TypedArray ta = context.getTheme().obtainStyledAttributes( 51 R.styleable.LeanbackGuidedStepTheme); 52 float percent = ta.getFloat(R.styleable.LeanbackGuidedStepTheme_guidedStepKeyline, 40); 53 ta.recycle(); 54 return percent; 55 } 56 57 @Override 58 protected void onLayout(boolean changed, int l, int t, int r, int b) { 59 super.onLayout(changed, l, t, r, b); 60 61 TextView titleView = (TextView) getRootView().findViewById(R.id.guidance_title); 62 TextView descriptionView = (TextView) getRootView().findViewById(R.id.guidance_description); 63 64 int mTitleKeylinePixels = (int) (getMeasuredHeight() * mTitleKeylinePercent / 100); 65 if (titleView != null && titleView.getParent() == this) { 66 LayoutParams lp = (LayoutParams) titleView.getLayoutParams(); 67 68 // To make the mid position of a text line match the key line, we need to delete the 69 // existing textview top margin, the icon height and the baseline offset for the text 70 // line. 71 int guidanceTextContainerTop = mTitleKeylinePixels - lp.topMargin; 72 73 // If there are more than 1 line, always make "second line baseline" match 74 // the key line. Otherwise, use the "first line baseline". 75 if (titleView.getLineCount() > 1) { 76 guidanceTextContainerTop -= titleView.getLayout().getLineBaseline(1); 77 } else { 78 guidanceTextContainerTop -= titleView.getLayout().getLineBaseline(0); 79 } 80 81 int offset = guidanceTextContainerTop; 82 titleView.offsetTopAndBottom(offset); 83 84 if (descriptionView != null && descriptionView.getParent() == this) { 85 descriptionView.offsetTopAndBottom(offset); 86 } 87 } 88 } 89} 90