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