gpt4 book ai didi

Android Studio ImageButton Tooltip has curious behavior if button is created in java source file(如果按钮是在Java源文件中创建的,Android Studio ImageButton工具提示会有奇怪的行为)

转载 作者:bug小助手 更新时间:2023-10-28 09:24:50 26 4
gpt4 key购买 nike



I'm coming back to Android programming now I'm retired, and have encountered many problems when preparing the skeleton of my application.

我现在退休了,又回到了Android编程,在准备应用程序的框架时遇到了很多问题。


I finally created successfully imageButtons by dynamically coding in java.

我最终通过用Java动态编码成功地创建了ImageButton。


buttons are placed inside an horizontal scroll view, and will be managed by the application (most of them will be configurable (visible or not) that's why I must create them dynamically.

按钮放置在水平滚动视图中,并将由应用程序管理(大多数按钮是可配置的(可见或不可见),这就是我必须动态创建它们的原因。


I had to migrate an old sample project to AppCompat to make that work, since I want to be able to use SVG files, the application will manage vectorial drawings.

我不得不将一个旧的样例项目迁移到AppCompat才能做到这一点,因为我希望能够使用SVG文件,该应用程序将管理矢量图形。


I'm using Android Studio 2021.1.1, AGP 7.1.3, Gradle 7.2

我使用的是Android Studio 2021.1.1、AGP 7.1.3、Gradle 7.2


The problem at this time is that dynamic buttons has a tooltip which is not correct, the tooltip appears over a larger frame, while created in the xml file it is correct.
This only occurs if I modify the Imagebutton Style with the coding below, otherwise the tootips are correct:

此时的问题是,动态按钮的工具提示不正确,工具提示显示在较大的框架上,而在XML文件中创建的工具提示是正确的。只有当我用下面的代码修改ImageButton样式时,才会出现这种情况,否则Totips是正确的:


left image: tooltip ok, right one: not ok
enter image description here

左图:工具提示正常,右图:不好


styles.xml:
<style name="ButtonJlb" parent="android:style/Widget.Holo.Light.ImageButton">
</style>

MainActivity.java
//create button with custom style
int buttonStyle = R.style.ButtonJlb;
ImageButton buttonView = new ImageButton(new ContextThemeWrapper(this, buttonStyle), null, buttonStyle);
//set image
buttonView.setImageResource(R.drawable.ic_android);
//set tooltip
btnSettings.setTooltipText("Settings");

Maybe my code is not correct somewhere ?
I didn't find documentation enough...

也许我的代码在某个地方不正确?我没有找到足够的文件...


I tried to use version 2022.3.1-19 of Android Studio, but I have only 8GB of memory in the computer, and when launching the emulator, AS crashes and disapears when memory used reaches 7.9GB.

我尝试使用Android Studio的版本2022.3.1-19,但我在计算机中只有8 GB的内存,当启动模拟器时,AS崩溃并在内存使用达到7.9GB时消失。


I have seen people complaining about very bad performance (long response time) of the new version, is it possible that memory could be the problem ?)

我看到人们抱怨新版本的性能非常差(响应时间很长),有没有可能是内存问题?)


I will purchase additional memory next week, but not sure it is an IDE issue.

我下周会购买额外的内存,但不确定是不是IDE的问题。


thanks in advance for any idea or solution...
Sorry if my post is not clear enough... let me know how to improve...

提前感谢您提出的任何想法或解决方案。如果我的帖子不够清楚,很抱歉。让我知道如何改进。


My API level is 17, I think, where can I find the current version in the settings ?
here is the xml code for buttons, only the first one is created in this example:

我的API级别是17,我想,在设置中哪里可以找到当前版本?下面是按钮的XML代码,在本例中只创建了第一个按钮:


    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">

<LinearLayout
android:id="@+id/linearLayoutBtn_top"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:orientation="horizontal">

<ImageButton
android:id="@+id/imageButton"
style="@style/ButtonJlb"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/ic_android" />
</LinearLayout>
</HorizontalScrollView>

</LinearLayout>

and this is the source code for dynamic creation (btnSettings is a reference to the first dynamically created button for next usage)

这是动态创建的源代码(btn设置是对第一个动态创建的按钮的引用,以供下一次使用)


        protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// scroll buttons creation
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutBtn_top);
for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
int buttonStyle = R.style.ButtonJlb;
ImageButton buttonView = new ImageButton(new ContextThemeWrapper(this, buttonStyle), null, buttonStyle);
buttonView.setImageResource(R.drawable.ic_android);
//change style of button
// fix width (1dp = 4px)
buttonView.setMinimumWidth(240);
buttonView.setTooltipText("btn"+i);
switch (i) {
case 1: {
btnSettings = buttonView;
buttonView.setTooltipText("Settings");
buttonView.setImageResource(R.drawable.ic_nand_generated);
break;
}
case 2: btnMap = buttonView; buttonView.setTooltipText("Map");break;
}
ImageButton firstbutton = findViewById(R.id.imageButton);
if (firstbutton != null) {
firstbutton.setTooltipText("First button");
}
layout.addView(buttonView, p);
};

// Click event on first button (settings)
btnSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startMapActivity();
}
});

Regarding Maurice Lam reply, if I just create buttons with this code

关于Maurice Lam的回复,如果我只是用这段代码创建按钮,


ImageButton buttonView = new ImageButton(this, null, buttonStyle);

this is what I get:

这就是我得到的:


enter image description here


更多回答

pls share your xml code for buttons , btnSettings is defined? and what is your android API level ?

请分享您的按钮的XML代码,btn设置是否已定义?您的Android API级别是多少?

I noticed that you specified the same thing in both the theme (in ContextThemeWrapper) and the style (in the 3rd argument to the constructor). To create in the same way as XML you typically just need the style not the theme

我注意到您在主题(在ConextThemeWrapper中)和样式(在构造函数的第三个参数中)中都指定了相同的内容。要以与XML相同的方式创建,您通常只需要样式而不是主题

I have added answers to my post, thank you

我已经在我的帖子中添加了回复,谢谢

优秀答案推荐
更多回答

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