Пользовательский прокручиваемый макет со значениями по умолчанию
Я пытаюсь сделать собственный RelativeLayout внутри ScrollView.
Теперь я должен скопировать эту часть кода в каждом месте, где я хочу его использовать:
<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>
Можно ли вообще так сделать?
Обновление 1Я попытался сделать это таким образом:
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 переопределяет себя. Как будто они игнорировалиandroid:layout_*
приписывать. Что я делаю неправильно?