我想为XML中的自定义视图指定子布局,类似于include元素的layout属性:
<include layout="@layout/all_apps_cling"
android:id="@+id/all_apps_cling"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
此外,我需要自定义视图在设计时在Eclipse或IntelliJ图形布局预览中呈现
使用attr name =“ android:layout”不起作用
[至少不在图形布局预览中]
attrs.xml
<declare-styleable name="ShowcaseView">
<attr name="android:layout"/>
</declare-styleable>
usecase.xml
<com.github.espiandev.showcaseview.ShowcaseView
style="@style/ShowcaseView"/>
styles.xml
<style name="ShowcaseView" parent="match_fill">
<item name="android:layout">@layout/handy</item>
</style>
ShowcaseView.java
public ShowcaseView(Context context) {
this(context, null, R.styleable.CustomTheme_showcaseViewStyle);
}
public ShowcaseView(Context context, AttributeSet attrs) {
this(context, attrs, R.styleable.CustomTheme_showcaseViewStyle);
}
public ShowcaseView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Get the attributes for the ShowcaseView
final TypedArray styled = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShowcaseView, 0, 0);
TypedValue handLayout = null;
// Not sure if should be using .getValue in the first place
// But there is no method TypedValue.getLayout or such
if (styled.getValue(R.styleable.ShowcaseView_android_layout, handLayout)) {
// Does not reach here
} else {
// Reaches here instead i.e. getValue failed
}
styled.recycle();
}
有什么办法吗?
而且我不能使用
<attr name="linkedLayout" format="reference"/>
,因为我需要它在图形布局预览中工作-在设计器中查看访问资源的问题Resources$NotFoundException in Graphical Layout ADT preview (but app actually Works) 最佳答案
尝试将布局放置在主布局文件中:
<ShowcaseView ...>
<include .../>
</ShowcaseView>
当然,您也可以直接添加子代,而不是添加标记:
<ShowcaseView ...>
<Button .../>
...
<Button .../>
</ShowcaseView>