Java 如何在每个文本视图后设置具有4个按钮的小部件工具栏
我正在设计一个quotes应用程序,我在一个页面(活动)中有这么多的quotes,所以我希望在每个textview之后都有一个小部件工具栏和4个共享按钮,我是通过xml实现的,我想通过java活动编码来实现这一点,这样编码长度就会减少,请为我提供正确的解决方案,请查看这些图片和我的代码Java 如何在每个文本视图后设置具有4个按钮的小部件工具栏,java,android,arrays,string,Java,Android,Arrays,String,我正在设计一个quotes应用程序,我在一个页面(活动)中有这么多的quotes,所以我希望在每个textview之后都有一个小部件工具栏和4个共享按钮,我是通过xml实现的,我想通过java活动编码来实现这一点,这样编码长度就会减少,请为我提供正确的解决方案,请查看这些图片和我的代码 如果要显示多个引号,我建议您使用RecyclerView以及RecyclerView.Adapter线性布局可能会减慢应用程序运行速度 但如果您的报价不太多,或者您确实想使用LinearLayout。您可以构建
如果要显示多个引号,我建议您使用
RecyclerView
以及RecyclerView.Adapter
<代码>线性布局可能会减慢应用程序运行速度
但如果您的报价不太多,或者您确实想使用LinearLayout
。您可以构建一个listitem\u quote.xml
首先包含您的报价和4个按钮
然后在activity.xml中使用空的LinearLayout
,如下所示
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/myLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
这样就行了
但是,我仍然建议您使用RecyclerView
。
你可以在网上找到许多例子。
它看起来更复杂,但很有用
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/myLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
onCreate(...){
setContentView(R.layout.activity_main);
myLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
LayoutInflater layoutInflater = LayoutInflater.from(this);
for(...){ // for every quote
// create a view for quote contains buttons
View quoteView = layoutInflater.inflate(R.layout.listitem_quote, null);
TextView tvQuote = quoteView.findViewById(...);
tvQuote.setText(...);
Button btn1 = quoteView.findViewById(...);
btn1.setOnclickListener(...);
myLinearLayout.addView(quote);// add to Linearlayout
}
}