java android 将按钮添加到标题栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17151722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:09:39  来源:igfitidea点击:

android add button to title bar

javaandroidandroid-layoutandroid-titlebar

提问by Arif YILMAZ

I am trying to add a button to title bar but I couldnt succeed. I have read many articles about this but they all explain the way that doesnt fit my situtation. I am using ScrollView to make the screen scrollable. but articles suggest me to use linear layout. How can I make it scrollable and have a button on title bar?

我正在尝试向标题栏添加一个按钮,但我无法成功。我已经阅读了很多关于此的文章,但它们都解释了不适合我的情况的方式。我正在使用 ScrollView 使屏幕可滚动。但文章建议我使用线性布局。如何使它可滚动并在标题栏上有一个按钮?

here is my xml

这是我的 xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#C0C0C0" 
android:id="@+id/sw_layout">

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:id="@+id/ln_layout">

    <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft=<"@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >
        .........................
        .........................
        .........................
    </TableLayout>
 </LinearLayout>
</ScrollView>

And in my Activity, I have this code....

在我的活动中,我有这个代码....

LinearLayout ln = (LinearLayout) getWindow().findViewById(R.id.ln_layout);
Button btn = new Button(this);
btn.setText("Test");
ln.addView(btn);

But this doesnt doesnt display anything and it doesnt give me any error. Please give me an idea. how can I add the button to title bar?

但这并没有显示任何内容,也没有给我任何错误。请给我一个想法。如何将按钮添加到标题栏?

回答by qwr

make title.xml and design your title : place buttonetc

制作 title.xml 并设计您的标题:放置按钮

on activity

在活动中

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

you will access to elements on title the same way you do on main layout .using findviewbyid

您将像在主布局上一样访问标题上的元素。使用 findviewbyid

for avoiding some look problems

避免一些外观问题

<style name="theme">
    <item name="android:windowTitleBackgroundStyle">@style/themeTitleBackground</item>
    <item name="android:windowTitleSize">65dip</item>

</style>

<style name="themeTitleBackground"> 
    <item name="android:layout_height">wrap_content</item>


</style>

inside androidmanifest

在androidmanifest里面

 <activity   android:theme="@style/theme"