【发布时间】:2019-01-16 12:07:42
【问题描述】:
我正在制作一个看起来像这样的聊天应用程序(虚拟视图):
按原样使用 xml 和活动
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="@+id/chat_message_HSV"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/send"
android:clickable="true" >
<LinearLayout
android:id="@+id/LL_inside_HSV"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
<ImageButton
android:id="@+id/send"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
我想向 HorizontalScrollView(HSV) 添加一个视图。我读过 HSV 只能有 1 个孩子(主要是 LinearLayout)& 必须添加视图。这是我要添加的视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75dp"
android:layout_height="75dp"
android:orientation="vertical"
android:background="#454545" >
<ImageView
android:id="@+id/profilePicture"
android:layout_width="60dp"
android:layout_height="60dp" />
<AutoCompleteTextView
android:layout_width="75dp"
android:layout_height="wrap_content" />
</LinearLayout>
这是我的活动:
public class MainActivity extends Activity {
ImageButton sendIB;
HorizontalScrollView chatMessageHSV;
LinearLayout lLinsideHSV;
View child;
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = inflater.inflate(R.layout.word_element, null, false);
sendIB = (ImageButton) findViewById(R.id.send);
chatMessageHSV = (HorizontalScrollView) findViewById(R.id.chat_message_HSV);
lLinsideHSV = (LinearLayout) findViewById(R.id.LL_inside_HSV);
chatMessageHSV.setOnClickListener(new OnClickListener() {
// this never gets triggered
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "hsv", Toast.LENGTH_SHORT)
.show();
}
});
lLinsideHSV.setOnClickListener(new OnClickListener() {
// this never gets triggered
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "LL", Toast.LENGTH_SHORT)
.show();
}
});
sendIB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
lLinsideHSV.addView(child);
}
});
}// end of onCreate
}
注意:我想在 HSV 的点击上添加视图。
Q-1:我无法触发 HSV 的 onClick 或其中的 LinearLayout。我尝试过 onTouchEvent 但每次触摸都会触发该事件 4-5 次。
Q-2 我编写了在按钮的 onClick 内插入视图的代码(只是为了实验)。我只能插入一次视图。之后它抛出一个异常说:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
那么我如何一次又一次地插入视图??
【问题讨论】:
-
在添加视图之前试试这个 parentview.removeView()
-
我应该删除哪个视图。这个函数需要一个 View 参数
-
child.getParent() 将为您提供父视图,您应先将其删除。
-
@stan0 : 已经回答了我的这部分问题...无论如何谢谢...请寻找问题1的解决方案
标签: android android-view horizontalscrollview