我正在尝试在ScrollView中创建自定义RelativeLayout。
现在,我必须在要使用它的每个位置复制这部分代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="xxx"
android:layout_height="yyy">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_margin="@dimen/card_margin"
android:background="@drawable/card_background"
android:gravity="center"
android:padding="@dimen/card_padding">
// content
</RelativeLayout>
</ScrollView>
我想这样得到相同的结果:
<MyScrollableRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="xxx"
android:layout_height="yyy">
// content
</MyScrollableRelativeLayout>
这样是否有可能做到这一点?
我试图以这种方式做到这一点:
public class MyScrollableRelativeLayout extends ScrollView {
private RelativeLayout content;
int padding;
int margin;
public MyScrollableRelativeLayout(Context context) {
super(context);
postConstruct();
}
public MyScrollableRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
postConstruct();
}
public MyScrollableRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
postConstruct();
}
private void postConstruct() {
float scale = getResources().getDisplayMetrics().density;
padding = (int) (5 * scale);
margin = (int) (20 * scale);
content = new RelativeLayout(getContext());
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(margin, margin, margin, margin);
addView(content, layoutParams);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
setLook();
}
private void setLook() {
setCardBackground();
setCardPadding();
}
private void setCardBackground() {
content.setBackground(getResources().getDrawable(R.drawable.card_background));
}
private void setCardPadding() {
setPadding(padding, padding, padding, padding);
}
@Override
public void addView(@NonNull View child) {
if (getChildCount() == 0) {
super.addView(child);
} else {
content.addView(child);
}
}
@Override
public void addView(@NonNull View child, int index) {
if (getChildCount() == 0) {
super.addView(child, index);
} else {
content.addView(child, index); }
}
@Override
public void addView(@NonNull View child, ViewGroup.LayoutParams params) {
if (getChildCount() == 0) {
super.addView(child, params);
} else {
content.addView(child, params);
}
}
@Override
public void addView(@NonNull View child, int index, ViewGroup.LayoutParams params) {
if (getChildCount() == 0) {
super.addView(child, index, params);
} else {
content.addView(child, index, params);
}
}
}
但是ScrollableCardRelativeLayout中的element中的问题已被自己覆盖。就像他们忽略了android:layout_*
属性。我究竟做错了什么?
您可以使用一些棘手的LayoutParams包装器,如下所示:
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
public class MyScrolledRelativeLayout extends ScrollView {
private static final String TAG = "MyScrolledRelativeLayout";
private RelativeLayout rl;
public MyScrolledRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
rl = new RelativeLayout(context);
addView(rl);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParamsWrapper(getContext(), attrs);
}
public void addView(View child, ViewGroup.LayoutParams params) {
Log.d(TAG, "addView " + child + " " + params);
if (getChildCount() != 0) {
LayoutParamsWrapper layoutParamsWrapper = (LayoutParamsWrapper) params;
rl.addView(child, layoutParamsWrapper.wrapped);
}
};
class LayoutParamsWrapper extends FrameLayout.LayoutParams {
private RelativeLayout.LayoutParams wrapped;
public LayoutParamsWrapper(Context c, AttributeSet attrs) {
super(c, attrs);
wrapped = new RelativeLayout.LayoutParams(c, attrs);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句