80,492
社区成员
发帖
与我相关
我的任务
分享
public class TopImageBtn extends LinearLayout {
public TopImageBtn(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TopImageBtn(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_topimagebtn, this);
TypedArray typedArray = context.obtainStyledAttributes(attrs ,R.styleable.TopImageBtn);
typedArray.recycle();
}
public TopImageBtn(Context context) {
super(context);
}
}
<?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:gravity="center_horizontal"
android:padding="5dp"
android:background="#5a6672"
android:orientation="vertical" >
<ImageView
android:id="@+id/TopImageBtn_Iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:contentDescription="TestDescription"
android:maxHeight="60dp"
android:maxWidth="60dp"
android:scaleType="centerInside"
android:src="@drawable/clipboard" />
<LinearLayout android:id="@+id/contentLayout">
<!-- 我想把内容放到这里来。如何实现 -->
</LinearLayout>
<TextView
android:id="@+id/TopImageBtn_Tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="12pt"
android:text="会员中心" />
</LinearLayout>
<com.example.btnapplication.TopImageBtn
android:id="@+id/TestTopImageBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
crlNameSpace:textSize="12pt"
android:layout_marginTop="10dp" >
<!-- 如何把这个 TextView 添加到 contentLayout 仲 -->
<TextView android:id="@+id/Test111" android:text="测试添加内容" />
</com.example.btnapplication.TopImageBtn>
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (getChildCount() > 1) {
View v2 = getChildAt(1);
if (v2 != null) {
removeViewAt(1);
}
View v1 = getChildAt(0);
if (v1 instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v1;
if (v2 != null) {
vg.addView(v2);
}
}
}
super.onLayout(changed, left, top, right, bottom);
}
<com.example.btnapplication.TopImageBtn
android:id="@+id/TestTopImageBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
crlNameSpace:textSize="12pt"
android:layout_marginTop="10dp" >
<!-- 如何把这个 TextView 添加到 contentLayout 仲 -->
<TextView android:id="@+id/Test111" android:text="测试添加内容" />
<TextView android:id="@+id/Test222" android:text="测试添加内容222" />
<ImageView android:id="@+id/Test333"></ImageView>
</com.example.btnapplication.TopImageBtn>