android 继承 LinearLayout 自定义 容器问题

HYH_HENG 2015-06-08 09:58:56
我新建了一个 类并继承了 LinearLayout 类 在 构造函数里 应用了 布局文件 view_topimagebtn.xml 如下

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);
}
}


在我的 view_topimagebtn.xml 中 存在 一个 id 叫 contentLayout 的 LinearLayout

<?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>


在activity 的 xml 仲如下使用

<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>


问题就是 如何把这个 TextView(id:Test111) 添加到 LinearLayout (id:contentLayout )

其实我就是想定义一个模板。把头尾都设置好样式。不过内容要可以随意添加内容的
...全文
647 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccpat 2015-06-11
  • 打赏
  • 举报
回复
插入到第一个child的中间位置用 vg.addView(v2, 1);
引用 11 楼 ccpat 的回复:
重写 TopImageBtn的onLayout方法,按照你贴出来的例子,在onLayout中getChildCount()应该等于2,第一个child是LinearLayout,第二个child是你要插入的TextView。把这个要插入的TextView删掉,然后插入到第一个child合适的位置就可以了。

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);
	}
ccpat 2015-06-11
  • 打赏
  • 举报
回复
重写 TopImageBtn的onLayout方法,按照你贴出来的例子,在onLayout中getChildCount()应该等于2,第一个child是LinearLayout,第二个child是你要插入的TextView。把这个要插入的TextView删掉,然后插入到第一个child合适的位置就可以了。

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);
	}
HYH_HENG 2015-06-09
  • 打赏
  • 举报
回复
谢谢大家的回答
HYH_HENG 2015-06-08
  • 打赏
  • 举报
回复
我之前搞过 .net 的 WPF 开发。我是把 WPF 的开发思想 放到 android 来。不知道如何实现
tcmakebest 2015-06-08
  • 打赏
  • 举报
回复
界面XML文件要么是在设计的时候拖控件进去,不然就在运行时用代码控制,还有什么办法
栀暖莺飞 2015-06-08
  • 打赏
  • 举报
回复
fragment对应任意你想要的xml,不需要自定义容器。
栀暖莺飞 2015-06-08
  • 打赏
  • 举报
回复
你的意思是上下固定,中间内容随便换么?
上下固定写在activity的xml里面,然后写你想要的任何fragment添加到activity显示内容的容器里面就好了。
YaphetS_Jo 2015-06-08
  • 打赏
  • 举报
回复
引用 5 楼 YaphetS_Jo 的回复:
[quote=引用 3 楼 neicunka 的回复:] [quote=引用 2 楼 YaphetS_Jo 的回复:] contentLayout.addView(new TextView())
这个办法我试过,是可以的。但是不是我写要的效果。我想要的是在XML中实现自动添加到我的 LinearLayout(id:contentLayout)不是在代码里面写。 因为 1 作出一个模板页。可能很多地方用到。在代码写的以后维护会乱 2 如果我的子内容不止一个TextView ,在代码里写也是不符合实际[/quote] 那你就再写一个xml放textview,通过inflate填充,然后再addview View view = LayoutInflater.from(this).inflate(R.layout.xxx, null); contentLayout.addView(view);
YaphetS_Jo 2015-06-08
  • 打赏
  • 举报
回复
引用 3 楼 neicunka 的回复:
[quote=引用 2 楼 YaphetS_Jo 的回复:] contentLayout.addView(new TextView())
这个办法我试过,是可以的。但是不是我写要的效果。我想要的是在XML中实现自动添加到我的 LinearLayout(id:contentLayout)不是在代码里面写。 因为 1 作出一个模板页。可能很多地方用到。在代码写的以后维护会乱 2 如果我的子内容不止一个TextView ,在代码里写也是不符合实际[/quote] 那你就再写一个xml放textview,通过inflate填充,然后再addview View view = LayoutInflater.from(this).inflate(R.id.xxx, null); contentLayout.addView(view);
HYH_HENG 2015-06-08
  • 打赏
  • 举报
回复
引用 1 楼 neicunka 的回复:
我之前搞过 .net 的 WPF 开发。我是把 WPF 的开发思想 放到 android 来。不知道如何实现
我要的效果是 只要我这么写 子内容都依次添加到我的 LinearLayout(id:contentLayout)

 <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>
HYH_HENG 2015-06-08
  • 打赏
  • 举报
回复
引用 2 楼 YaphetS_Jo 的回复:
contentLayout.addView(new TextView())
这个办法我试过,是可以的。但是不是我写要的效果。我想要的是在XML中实现自动添加到我的 LinearLayout(id:contentLayout)不是在代码里面写。 因为 1 作出一个模板页。可能很多地方用到。在代码写的以后维护会乱 2 如果我的子内容不止一个TextView ,在代码里写也是不符合实际
YaphetS_Jo 2015-06-08
  • 打赏
  • 举报
回复
contentLayout.addView(new TextView())

80,492

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧