我通过夸大布局来调暗视图,代码如下:
public class MenuItemView extends RelativeLayout {
private Context context;
TextView tv_title;
String title = "";
TextView tv_content;
String content = "";
TextView tv_guid;
String guid = "";
ImageView iv_divider;
ImageView iv_arrow;
RelativeLayout rl_main;
public MenuItemView(Context context) {
super(context);
initView(context, null);
}
public MenuItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public MenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
public MenuItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initView(context, attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).layout(l, t, r, b);
}
}
private void initView(Context context, AttributeSet attrs) {
this.context = context;
inflate(context, R.layout.menu_item_view_layout, this);
tv_title = (TextView) findViewById(R.id.tv_title);
tv_content = (TextView) findViewById(R.id.tv_content);
tv_guid = (TextView) findViewById(R.id.tv_guid);
iv_arrow = (ImageView) findViewById(R.id.iv_arrow);
iv_divider = (ImageView) findViewById(R.id.iv_divider);
rl_main = (RelativeLayout) findViewById(R.id.rl_main);
//Default
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MenuItemView);
title = a.getString(R.styleable.MenuItemView_menu_title);
content = a.getString(R.styleable.MenuItemView_menu_content);
guid = a.getString(R.styleable.MenuItemView_menu_guid);
a.recycle();
}
//
tv_title.setText(title);
tv_content.setText(content);
tv_guid.setText(guid);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private int getpx(int dp) {
return (int) (context.getResources().getDisplayMetrics().density * dp + 0.5f);
}
}
当我在layout.like这个
中使用它时虽然执行得很好,但是当我有时使用它时,就像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<setmenu.smzdm.com.menuitemview.MenuItemView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu_content="Content"
app:menu_guid="Guid"
app:menu_title="Title" />
<setmenu.smzdm.com.menuitemview.MenuItemView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu_content="Content"
app:menu_guid="Guid"
app:menu_title="Title" />
<setmenu.smzdm.com.menuitemview.MenuItemView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu_content="Content"
app:menu_guid="Guid"
app:menu_title="Title" />
</LinearLayout>
它只能显示一个视图,例如:
当它在移动设备上运行时,它也可以在布局中显示一个视图。
答案 0 :(得分:0)
您的扩充视图未添加到您的客户视图中。你应该修理:
View view = inflate(context, R.layout.menu_item_view_layout, this);
tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_content = (TextView) view.findViewById(R.id.tv_content);
tv_guid = (TextView) view.findViewById(R.id.tv_guid);
iv_arrow = (ImageView) view.findViewById(R.id.iv_arrow);
iv_divider = (ImageView) view.findViewById(R.id.iv_divider);
rl_main = (RelativeLayout) view.findViewById(R.id.rl_main);
// your code
// and last, you have to add view to customeView
addView(view);