300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > android标题栏上增加按钮 Android:向自定义标题栏添加按钮

android标题栏上增加按钮 Android:向自定义标题栏添加按钮

时间:2019-07-11 04:04:30

相关推荐

android标题栏上增加按钮 Android:向自定义标题栏添加按钮

我创建了一个自定义标题栏,如本例所示

“一个自定义标题栏” – 一半下来.

在某些活动中,我想在标题栏的右侧放置一个按钮(与facebook app相同).我试图按如下方式向视图添加一个按钮,但它没有出现.

自定义标题栏显示如下

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

的setContentView(R.layout.maintabhost);

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.headerbar_include);

试图按如下方式添加按钮.该按钮最终将是一个ImageButton并与自定义标题栏的右侧对齐 – 如果我让它工作. (刚才意识到我现在有太多的布局,但这不会影响按钮显示)

LinearLayout layout = (LinearLayout) findViewById(R.id.headerbar);

Button searchButton = new Button(this);

searchButton.setText("info");

LayoutParams layoutParams = new LinearLayout.LayoutParams

(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

searchButton.setLayoutParams(new LayoutParams(

ViewGroup.LayoutParams.WRAP_CONTENT,

ViewGroup.LayoutParams.WRAP_CONTENT));

layout.addView(searchButton, layoutParams);

layout.invalidate();

我可以使用已嵌入的按钮创建另一个自定义标题栏,但动态解决方案会更好.

干杯

解决方法:

首先,感谢我的博客链接.其次,让我看看我是否能为你回答这个问题.当你想添加一个按钮时,你在添加另一个按钮时遇到问题的原因之一是,在那个例子中,我让你无法通过常规频道检索标题栏视图.因此,让我们解决它(并且可能让我在下周末写另一篇博文.)

从xml文件开始,添加id属性:

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_horizontal"

android:orientation="horizontal"

android:id="@+id/title_complex">

这里的代码向您展示如何在Activity中获取该按钮(稍后您将需要添加所有的天赋):

LinearLayout layout = (LinearLayout) getWindow().findViewById(R.id.title_complex);

layout.addView(new Button(this));

如果你看看,标题栏中有一个非描述性的按钮(就像我说的,你必须添加自己的天赋):

但请注意,我不能保证按钮将保留或不会保留在后续活动中.我还没有调查过,但这应该让你开始.如果您还有其他问题,请随时在这里(更多眼球)或我的博客上询问.

标签:android,button,titlebar

来源: https://codeday.me/bug/0610/1211108.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。