Android getChildAt()在自定义视图组构造函数中不起作用
我正在创建一个类似复合的视图。这里是like_layout_1.xml:Android getChildAt()在自定义视图组构造函数中不起作用,android,android-layout,android-linearlayout,android-view,android-custom-view,Android,Android Layout,Android Linearlayout,Android View,Android Custom View,我正在创建一个类似复合的视图。这里是like_layout_1.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:l
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="@drawable/like" />
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="12569"
android:textSize="20px" />
</LinearLayout>
public class LikeView extends LinearLayout {
private ImageView likeImageView;
private TextView likeCountTextView;
private boolean isLiked;
private long likeCount;
public LikeView(Context context) {
this(context, null);
}
public LikeView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LikeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.like_layout_1, this);
likeImageView = (ImageView) getChildAt(0);
likeCountTextView = (TextView) getChildAt(1);
}
}
由于这两条线,会发生充气异常
likeImageView = (ImageView) getChildAt(0);
likeCountTextView = (TextView) getChildAt(1);
方法getChildAt()会产生问题,因为如果我对这两行进行注释,则不会出现异常,并且工作正常
注意:我不想使用findByView()方法访问子级
我正在创建一个类似复合的视图
您是否尝试过[android:drawableLeft][1]
或[android:drawableRight][2]
例如
应该是
已编辑:不在类中使用或导入ID或应用程序R
循环和你即兴创作的所有逻辑
@Override
public void addView(View child, int index,
android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
// If the view hierarchy is fixed and limitted thn you can also check
// with the index
// but from constructor you wont get instance because first parent will
// be loaded and child may not be loaded
// TO handle it from index
int size = getChildCount();
for (int i = 0; i < size; i++) {
switch (i) {
case 0:
if (TextView.class.isInstance(getChildAt(0)))
likeCountTextView = (TextView) getChildAt(0);
break;
case 1:
if (TextView.class.isInstance(getChildAt(1)))
likeCountTextView = (TextView) getChildAt(1);
break;
case 2:
if (SeekBar.class.isInstance(child)) {
SeekBar seekbar = (SeekBar) child;
}
break;
case 3:
if (ImageView.class.isInstance(child)) {
likeImageView = (ImageView) child;
}
break;
case 4:
if (Button.class.isInstance(child)) {
Button btnFirst = (Button) child;
}
break;
case 5:
if (Button.class.isInstance(child)) {
Button btnSecond = (Button) child;
}
break;
}
}
// int id = child.getId();
// if (id == 0)
// return;
// switch (id) {
// case R.id.seekbar:
// // cast here with seekbar
// break;
// case R.id.txtTouchEventTest:
// likeCountTextView = (TextView) child;
// break;
// case R.id.imageView:
// likeImageView = (ImageView) child;
// break;
//
// case R.id.show_notification:
// // cast here with button
// break;
// case R.id.hide_notification:
// // cast here with button
// break;
// }
}
@覆盖
public void addView(视图子级、整数索引、,
android.view.ViewGroup.LayoutParams(参数){
super.addView(子级、索引、参数);
//如果视图层次结构是固定的和受限的,您也可以检查
//使用索引
//但从构造器中你们不会得到实例,因为第一个父代会
//已加载,但不能加载子项
//从索引中处理它
int size=getChildCount();
对于(int i=0;i
我使用过的XML文件
<?xml version="1.0" encoding="utf-8"?>
<com.hiddenbrains.lib.emailutility.LikeView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTouchEventTest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:gravity="center"
android:padding="20dp"
android:text="Hello World"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/textViewSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:gravity="center"
android:padding="20dp"
android:text="TextView Second"
android:textColor="@android:color/black" />
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:max="100" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/show_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/hide_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</com.hiddenbrains.lib.emailutility.LikeView>
检查控件的自定义
希望我没弄错你的问题。。如果没有,请告诉我 你可以试试这个
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
likeImageView = (ImageView) getChildAt(0);
likeCountTextView = (TextView) getChildAt(1);
}
我也有同样的问题。我的解决办法如下:
likeImageView = (ImageView) getChildAt(1);
likeCountTextView = (TextView) getChildAt(2);
因为如果您执行
getChildAt(0)
和getChildAt(1)
操作,它会将图像视图膨胀到父线性布局您确定您在说什么吗?我像_layout_1.xml一样膨胀。不需要像com.yourpackage.like那样写查看我正在膨胀的视图。我们需要像com.yourpackage.LikeView那样编写我们将在哪个布局中使用它LikeView@mahbub.kuet对您需要使用addView方法,在那里您可以有子视图实例。。@mahbub.kuet请使用帮助链接检查更新的答案仍然发生相同的异常。您知道findViewById()在构造函数中工作,但getChildAt()不工作。您可以尝试在onFinishInflate方法中获取子对象,但不确定它是否工作。另外,应该有第三个参数用于方法充气,请尝试传递true。
@Override
public void addView(View child, int index,
android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
// If the view hierarchy is fixed and limitted thn you can also check
// with the index
// but from constructor you wont get instance because first parent will
// be loaded and child may not be loaded
// TO handle it from index
int size = getChildCount();
for (int i = 0; i < size; i++) {
switch (i) {
case 0:
if (TextView.class.isInstance(getChildAt(0)))
likeCountTextView = (TextView) getChildAt(0);
break;
case 1:
if (TextView.class.isInstance(getChildAt(1)))
likeCountTextView = (TextView) getChildAt(1);
break;
case 2:
if (SeekBar.class.isInstance(child)) {
SeekBar seekbar = (SeekBar) child;
}
break;
case 3:
if (ImageView.class.isInstance(child)) {
likeImageView = (ImageView) child;
}
break;
case 4:
if (Button.class.isInstance(child)) {
Button btnFirst = (Button) child;
}
break;
case 5:
if (Button.class.isInstance(child)) {
Button btnSecond = (Button) child;
}
break;
}
}
// int id = child.getId();
// if (id == 0)
// return;
// switch (id) {
// case R.id.seekbar:
// // cast here with seekbar
// break;
// case R.id.txtTouchEventTest:
// likeCountTextView = (TextView) child;
// break;
// case R.id.imageView:
// likeImageView = (ImageView) child;
// break;
//
// case R.id.show_notification:
// // cast here with button
// break;
// case R.id.hide_notification:
// // cast here with button
// break;
// }
}
<?xml version="1.0" encoding="utf-8"?>
<com.hiddenbrains.lib.emailutility.LikeView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTouchEventTest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:gravity="center"
android:padding="20dp"
android:text="Hello World"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/textViewSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:gravity="center"
android:padding="20dp"
android:text="TextView Second"
android:textColor="@android:color/black" />
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:max="100" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/show_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/hide_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</com.hiddenbrains.lib.emailutility.LikeView>
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
likeImageView = (ImageView) getChildAt(0);
likeCountTextView = (TextView) getChildAt(1);
}
likeImageView = (ImageView) getChildAt(1);
likeCountTextView = (TextView) getChildAt(2);