这是custom compound component扩展RelativeLayout
并从xml扩展特定布局:
public class MyCustomView extends RelativeLayout {
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.my_custom_view, this);
// ...
}
}
布局xml使用<merge>
标签(从视图层次中移除不必要的层,yada yada yada):
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" ... />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" ... />
...
</merge>
...我在其他布局中使用自定义视图:
<com.example.MyCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dip" />
自定义视图中的所有内容都是完美无瑕的,但 20dip padding 应该包含整个内容不。如果我把它放在<merge>
标签上似乎也不起作用。缺少填充使得它看起来完全糟糕。我应该在哪里放置android:padding="20dip"
属性来应用它?
最简单的方法可能就是让MyCustomView
扩展FrameLayout
,并将<merge>
标记替换为<RelativeLayout>
- 但这会牺牲整个'保持视图Romain Guy非常重视的层次结构:“
答案 0 :(得分:7)
现在我使用解决方法以编程方式设置填充。并寻找可能更好的解决方案。
// Inflate the view from the layout resource.
LayoutInflater li = LayoutInflater.from( context );
View root = li.inflate( R.layout.article_head, this, true );
int padding = convertDip2Pixels( context, 8 );
root.setPadding( padding, padding, padding, padding );
public static int convertDip2Pixels( Context context, int dip ) {
return (int)TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP,
dip, context.getResources().getDisplayMetrics() );
答案 1 :(得分:0)
我有类似的问题,填充没有转移。您是否尝试过使用保证金?