如何在自定义ViewGroup中膨胀RelativeLayout
我有一个自定义动画 ViewGroup (很大程度上基于 滑动选项卡)。
在我的应用程序中,我将其简化为单个 TextView,其行为就像 SlidingTab(锁定屏幕上的滑块)。这很好用,但是滑动的 TextView 是在代码中定义的。我想要一个完整的相对布局来滑动。
公共类 SlindingView 扩展 ViewGroup {
private static Slider mLeftSlider;
private static int tabWidth;
/**
* Simple container class for all things pertinent to a slider.
*/
private static class Slider {
private final TextView tab;
Slider(ViewGroup parent) {
// Want to change 'tab' to a RelativeLayout in xml
tab = new TextView(parent.getContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT));
tab.setText("SLIDE THIS");
parent.addView(tab);
}
void layout(int l, int t, int r, int b) {
tabWidth = getTabWidth();
int tabHeight = getTabHeight();
tab.layout(0, 0, tabWidth, tabHeight);
}
public void measure() {
tab.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
}
public int getTabWidth() {
return tab.getMeasuredWidth();
}
public int getTabHeight() {
return tab.getMeasuredHeight();
}
}
/**
* Constructor
*/
public SlindingView(Context context, AttributeSet attrs) {
super(context, attrs);
mLeftSlider = new Slider(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
mLeftSlider.measure();
final int leftTabWidth = mLeftSlider.getTabWidth();
final int leftTabHeight = mLeftSlider.getTabHeight();
final int width = Math.max(widthSpecSize, leftTabWidth);
final int height = leftTabHeight;
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mLeftSlider.layout(l, t, r, b);
}
}
I have a custom animated ViewGroup (based heavily on SlidingTab).
In my app, I've stripped this down to just a single TextView that behaves just like the SlidingTab (the slider on your lock screen). This works great, but the TextView that slides is defined in code. I'd like to have an entire RelativeLayout that slides instead.
I have looked at all sorts of posts trying different things but still haven't gotten it to work. The RelativeLayout xml file has to be inflated and added as a child of 'parent' in the Slider constructer, but I also need to be able do the measure() and layout() methods. See the relevant code below.
public class SlindingView extends ViewGroup {
private static Slider mLeftSlider;
private static int tabWidth;
/**
* Simple container class for all things pertinent to a slider.
*/
private static class Slider {
private final TextView tab;
Slider(ViewGroup parent) {
// Want to change 'tab' to a RelativeLayout in xml
tab = new TextView(parent.getContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT));
tab.setText("SLIDE THIS");
parent.addView(tab);
}
void layout(int l, int t, int r, int b) {
tabWidth = getTabWidth();
int tabHeight = getTabHeight();
tab.layout(0, 0, tabWidth, tabHeight);
}
public void measure() {
tab.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
}
public int getTabWidth() {
return tab.getMeasuredWidth();
}
public int getTabHeight() {
return tab.getMeasuredHeight();
}
}
/**
* Constructor
*/
public SlindingView(Context context, AttributeSet attrs) {
super(context, attrs);
mLeftSlider = new Slider(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
mLeftSlider.measure();
final int leftTabWidth = mLeftSlider.getTabWidth();
final int leftTabHeight = mLeftSlider.getTabHeight();
final int width = Math.max(widthSpecSize, leftTabWidth);
final int height = leftTabHeight;
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mLeftSlider.layout(l, t, r, b);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论