问题描述
我需要一个 ListActivity 和一个 ListView,它下面有一个 Button。因此,您可以在 Button 停留在底部时滚动 ListView。布局看起来不错,但是当我启动应用程序时,没有按钮。 那是我的 ListActivity 的 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
tools:context=".PalettenActivity" >
<ListView
android:id="@+id/listViewPaletten"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@+id/buttonPalettenBetaetigen"
android:entries="@layout/paletten_item"
android:minHeight="?android:attr/listPreferredItemHeight" >
</ListView>
<Button
android:id="@+id/buttonPalettenBetaetigen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Bestätigen" />
</RelativeLayout>
这是paletten_item的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutPalettenItem"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
tools:context=".Checklistadapter" >
<TextView
android:id="@+id/textViewPalettenBez"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="left|center_vertical"
android:text="Bezeichnung"
android:textSize="25sp" />
<Button
android:id="@+id/buttonPalettenPlus"
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="+"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewPalettenAnzahl"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignBaseline="@+id/buttonPalettenPlus"
android:layout_alignBottom="@+id/buttonPalettenPlus"
android:layout_toLeftOf="@+id/buttonPalettenPlus"
android:gravity="center"
android:text="0"
android:textSize="20sp" />
<Button
android:id="@+id/buttonPalettenMinus"
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignBaseline="@+id/textViewPalettenAnzahl"
android:layout_alignBottom="@+id/textViewPalettenAnzahl"
android:layout_toLeftOf="@+id/textViewPalettenAnzahl"
android:text="-"
android:textSize="20sp" />
</RelativeLayout>
这是我在 ListActivity 的 onCreate() 方法中将 onClickListener 添加到按钮的方法:
LayoutInflater itemInflater (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = itemInflater.inflate(R.layout.activity_paletten,null);
btnPalettenBestaetigen = (Button)convertView.findViewById(R.id.buttonPalettenBetaetigen);
btnPalettenBestaetigen.setText("Bestätigen");
btnPalettenBestaetigen.setVisibility(View.VISIBLE);
btnPalettenBestaetigen.setEnabled(true);
btnPalettenBestaetigen.setClickable(true);
btnPalettenBestaetigen.setonClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// do stuff
} catch (Exception e) {
Log.e(TAG,e.getMessage());
e.printstacktrace();
}
}
});
这是我的布局,按钮可见: Layout of my ListActivity
为什么当我启动应用程序时按钮不可见? 我很高兴得到任何帮助!
您好 莫里斯 F.
解决方法
SirGrey 最终是对的。
那一定是你的代码有问题......但我看不出问题,如果活动什么也没显示,一定是充气机有问题...... – SirGery
问题是充气机,所以我对我的代码做了以下更改:
-
我从 MainActivity 的 XML 中删除了
android:entries="@layout/paletten_item"
-
MainActivity 现在是
extends Activity
而不是ListActivity
-
我在 MainActivity 的
setContentView()
方法中onCreate()
-
我通过执行
获得 ListViewlistViewPaletten = (ListView)findViewById(R.id.listViewPaletten);
-
而且我不再使用 Infalter。
非常感谢您的帮助!
,如果您想滚动 ListView
同时按钮保持在同一位置,请尝试使用 FloatingActionButton
,它必须解决您的问题
<ListView
android:id="@+id/listViewPaletten"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:entries="@layout/paletten_item"
android:minHeight="?android:attr/listPreferredItemHeight" >
</ListView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fActionButton"
android:layout_width="180dp"
android:layout_height="180dp"
tools:ignore="MissingConstraints,VectorDrawableCompat"/>
,
然后试试这个,交换引用的活动,因为这是来自我的项目
import cairosvg
import io
import json
# fetch your JSON
json_string = web_fetch('https://example.com/captcha')
# parse it into a python object and extract the SVG document
svg = json.loads(json_string)['captcha']
# note: your SVG document declares its size to be 150x50 pixels,# which may be too small for some users to see; consider varying the `scale` parameter
# write image to file
cairosvg.svg2png(bytestring=svg,write_to='captcha_output.png',scale=3)
# or to memory to use it later
buf = io.BytesIO()
cairosvg.svg2png(bytestring=svg,write_to=buf,scale=3)