gpt4 book ai didi

android - ScrollView 在弹出窗口中不起作用

转载 作者:行者123 更新时间:2023-11-29 01:35:38 25 4
gpt4 key购买 nike

我有一个弹出窗口,并且添加了 ScrollView 作为其父布局。但它不起作用。如果我将 scrollview 更改为 diff 布局,它的工作..基本上我有多个布局(相对和线性的混合)并且我希望它可以滚动。我已经删除了复杂的树结构布局,所以让代码看起来很简单..

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >

<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/chatheadpop"
android:orientation="vertical" >

<TableRow
android:id="@+id/headerRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" >

<ImageView
android:id="@+id/profilePic"
android:layout_width="0dp"
android:layout_height="110dp"
android:layout_marginRight="10dp"
android:layout_weight="0.40"
android:background="#ccc" />

<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.60" >

<TextView
android:id="@+id/phoneIcon"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:color="#ccc" />

<TextView
android:id="@+id/phoneField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/phoneIcon"
android:layout_toRightOf="@+id/phoneIcon"
android:maxLines="1"
android:singleLine="true"
android:textSize="18sp"
android:color="#aaa" />

<TextView
android:id="@+id/nameIcon"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/phoneIcon"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:color="#ccc" />

<TextView
android:id="@+id/nameField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/nameIcon"
android:layout_toRightOf="@+id/nameIcon"
android:maxLines="1"
android:singleLine="true"
android:textSize="18sp"
android:color="#aaa" />

<TextView
android:id="@+id/locationIcon"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/nameIcon"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:visibility="invisible"
android:color="#ccc" />

<TextView
android:id="@+id/locationField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/locationIcon"
android:layout_toRightOf="@+id/locationIcon"
android:maxLines="1"
android:singleLine="true"
android:textSize="16sp"
android:visibility="invisible"
android:color="#aaa" />

<TextView
android:id="@+id/jobIcon"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/locationIcon"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:visibility="invisible"
android:color="#ccc" />

<TextView
android:id="@+id/jobField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/jobIcon"
android:layout_toRightOf="@+id/jobIcon"
android:maxLines="1"
android:singleLine="true"
android:textSize="16sp"
android:visibility="invisible"
android:color="#aaa" />
</RelativeLayout>
</TableRow>
</LinearLayout>
</ScrollView>

创建弹出窗口的代码

private void initiatePopupWindow(View anchor, final int pos) {
try {

popupWindow.dismiss();

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

try {
//Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.chat_popup, null);
//popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow = new PopupWindow(popupView, width_px, LayoutParams.WRAP_CONTENT);
//popupWindow.setBackgroundDrawable(new ColorDrawable());
//popupWindow.setFocusable(true);
//popupWindow.setOutsideTouchable(true);

profilePic = (ImageView) popupView.findViewById(R.id.profilePic);
phoneIcon = (TextView) popupView.findViewById(R.id.phoneIcon);

......
......
......
}

最佳答案

好的,这里也提示你可能需要展示它

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
popupWindow.showAsDropDown(context.getCurrentFocus(), 0, 0,Gravity.CENTER_HORIZONTAL);
}else{
popupWindow.showAtLocation(context.getCurrentFocus(), Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 10, 10);
}

编辑 1

TableRow 应该在 TableLayout 中使用,但没有限制,如果您在任何其他布局中将它作为子级使用,那么它会作为水平 linearLayout.. 所以既然你有各种各样的问题,我想你的问题源于你的 java 代码弹出一个 NPE 但你从未打印错误所以你没有真的没有真正看到它,或者可能是框架将您的 xml 小部件创建到内存中。所以这是您的解决方案,它会起作用,

我更改了 xml 中的某些部分,但您可以保留您的版本,但我的版本在布局方面符合您的要求,但具有不同的 View 组

XML 我称它为 popmenuitem.xml

  <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/headerRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:weightSum="10" >

<ImageView
android:id="@+id/profilePic"
android:layout_width="0dp"
android:layout_height="110dp"
android:layout_marginRight="10dp"
android:layout_weight="4"
android:background="#ccc" />

<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6" >

<TextView
android:id="@+id/phoneIcon"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:color="#ccc" />

<TextView
android:id="@+id/phoneField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/phoneIcon"
android:layout_toRightOf="@+id/phoneIcon"
android:maxLines="1"
android:singleLine="true"
android:textSize="18sp"
android:color="#aaa" />

<TextView
android:id="@+id/nameIcon"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/phoneIcon"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:color="#ccc" />

<TextView
android:id="@+id/nameField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/nameIcon"
android:layout_toRightOf="@+id/nameIcon"
android:maxLines="1"
android:singleLine="true"
android:textSize="18sp"
android:color="#aaa" />

<TextView
android:id="@+id/locationIcon"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/nameIcon"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:visibility="invisible"
android:color="#ccc" />

<TextView
android:id="@+id/locationField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/locationIcon"
android:layout_toRightOf="@+id/locationIcon"
android:maxLines="1"
android:singleLine="true"
android:textSize="16sp"
android:visibility="invisible"
android:color="#aaa" />

<TextView
android:id="@+id/jobIcon"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/locationIcon"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:visibility="invisible"
android:color="#ccc" />

<TextView
android:id="@+id/jobField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/jobIcon"
android:layout_toRightOf="@+id/jobIcon"
android:maxLines="1"
android:singleLine="true"
android:textSize="16sp"
android:visibility="invisible"
android:color="#aaa" />
</RelativeLayout>
</LinearLayout>

</LinearLayout>

</ScrollView>

然后在您的 Activity 中,请注意我以一种笨拙的方式对其进行了测试..但是您可以做一些小的更改。

Activity 代码

  //called this in my oncreate
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
initiatePopupWindow(getWindow().getDecorView(), 0, MainActivity.this);// i left pos because i didnt use it
}
}, 5000); // waited 5 seconds to notice it

// I AM OUT OF MY ONCREATE METHOD AFTER THIS LINE

@SuppressLint("NewApi")
private void initiatePopupWindow(View anchor, final int pos,Activity a) {

PopupWindow popupWindow = null; // i know the initialisation is outside this method but since
// i am just throwing an example i put it here..
View popupView;

if(popupWindow != null){ // the unreachable error being thrown here is the tautology code i just did
// but if you remove the initialisation from this method and place it outside the error will be gone
popupWindow.dismiss(); //
} // with this you get to remove the exception catch clause, and make your code look like you know how it works

//
try {
//Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
popupView = a.getLayoutInflater().inflate(R.layout.popmenuitem, null);
//popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow = new PopupWindow(a);
popupWindow.setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(FrameLayout.LayoutParams.WRAP_CONTENT);
// i used FrameLayout.LayoutParams because of the scrollView, this was some of the reasons i think might be the reason of the error, might
// not be though, but when incorporated everything seems cool..ScrollView extends FrameLayout
popupWindow.setContentView(popupView);
//popupWindow.setBackgroundDrawable(new ColorDrawable());
//popupWindow.setFocusable(true);
//popupWindow.setOutsideTouchable(true);
//profilePic = (ImageView) popupView.findViewById(R.id.profilePic);
//phoneIcon = (TextView) popupView.findViewById(R.id.phoneIcon);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
popupWindow.showAsDropDown(anchor, 0, 0,Gravity.CENTER_HORIZONTAL);
}else{
popupWindow.showAtLocation(anchor, Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 10, 10);
}
}catch(NullPointerException e){
e.printStackTrace();
}
}

快乐编码先生..

关于android - ScrollView 在弹出窗口中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28304788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com