사용자 정의 복합보기를 만들었습니다.보기가로드되고 충돌이 발생하지 않고 지금까지는 좋았습니다.사용자 정의보기 스타일, 안드로이드의 속성이 무시됩니다.
이제 뷰를 스타일이 필요하므로 내 앱에서이 뷰를 여러 번 사용할 계획입니다.
나는<declare-styleable name="MyCustomView">
<attr name="ff_label" format="string" />
<attr name="ff_fieldText" format="string" />
</declare-styleable>
<declare-styleable name="MyCustomViewStyle">
<attr name="customViewStyle" format="reference" />
</declare-styleable>
그럼 난 내 테마 파일에 가서 내 attr.xml 파일에의 styleable을 선언하고 내 안드로이드 매니페스트에 다음
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
//bunch of other stuff
<item name="customViewStyle">@style/customViewStyle</item>
</style>
내부 쓴 내가
선언<application
android:name="com.my.app.App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
내 styles.xml 파일에 다음과 같이 썼습니다.
<style name="customViewStyleStyle" parent="@android:style/Widget.EditText">
<item name="android:paddingBottom">@dimen/space_between_adjacent_widgets_vertical</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="ff_label">@string/default_label_text</item>
<item name="ff_fieldText">@string/default_label_text</item>
</style>
내 문제 : 내 자신의 특성을 잘 인식하고 있습니다.
"android:..."
레이블이 지정된 속성이 무시되는 이유는 무엇입니까?
MyCustomView.java
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initAttributes(context, attrs, R.attr.customViewStyle);
}
public MyCustomView(Context context) {
super(context);
initAttributes(context, null, R.attr.customViewStyle);
}
private void initAttributes(Context context, AttributeSet attrs, int defStyle) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_view, this, true);
label = (TextView) findViewById(R.id.label);
formField = (EditText) findViewById(R.id.formField);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle,0);
if (a.hasValue(R.styleable.MyCustomView_ff_label)) {
labelText = a.getString(R.styleable.MyCustomView_ff_label);
label.setText(labelText);
}
if (a.hasValue(R.styleable.MyCustomView_ff_fieldText)) {
fieldText = a.getString(R.styleable.MyCustomView_ff_fieldText);
field.setHint(fieldText);
}
a.recycle();
}
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.my.app"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<include layout="@layout/layout_set_default" />
</TableLayout>
1 개보기의 기본 높이가 약 30 DP입니다 -> 그리고 난 그것을 위해 목록보기를 사용할 수 없습니다.
...
public MyCustomView(Context context, AttributeSet attrs) {
//Called by Android if <com.my.app.MyCustomView/> is in layout xml file without style attribute.
//So we need to call MyCustomView(Context context, AttributeSet attrs, int defStyle)
// with R.attr.customViewStyle. Thus R.attr.customViewStyle is default style for MyCustomView.
this(context, attrs, R.attr.customViewStyle);
}
public MyCustomView(Context context) {
this(context, null);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
//Called by Android if <com.my.app.MyCustomView/> is in layout xml with style attribute
// For example:
// <com.my.app.MyCustomView
// android:layout_width="match_parent"
// android:layout_height="wrap_content"
// style="@style/customViewStyleStyle"
// />
//
super(context, attrs, defStyle);
initAttributes(context, attrs, defStyle);
}
...
그런 다음 당신이 layout.xml
에서 스타일 속성을 사용할 수 있습니다 및 customViewStyle
는 MyCustomView
에도 기본 스타일이 될 것입니다 : 현재처럼 MyCustomView
의 코드를 변경해야 할 각 뷰 사이
[Android의 스타일 지정보기]에 대한 기사 (http://onetouchcode.com/2016/11/25/styling-custom-views-android/). – Shailendra