根据标题,我需要检索表示位于 ActionBar 中的 Activity 标题的 TextView 。我可以使用以下方法获取 ActionBar View :
private View getActionBarView() {
View v = mWindow.getDecorView();
int resId = getResources().getIdentifier("action_bar_container", "id", "android");
return v.findViewById(resId);
}
有谁知道 TextView 的 ID,以便我可以在 actionbar View 中找到 ViewById?
已解决: 好吧,我用同样的方法解决了,但是改变了 id:
private TextView getActionBarTitle() {
View v = mWindow.getDecorView();
int resId = getResources().getIdentifier("action_bar_title", "id", "android");
return v.findViewById(resId);
}
最佳答案
所有内置的 Android 布局都可以在 SDK 中的以下位置找到:
.../android-sdk/platforms/android-<API-VERSION>/data/res/layout/
我认为在这种情况下您正在寻找的是 action_bar_title_item.xml。
这是 v19 的:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:orientation="vertical"
android:paddingEnd="8dp">
<TextView android:id="@+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end" />
<TextView android:id="@+id/action_bar_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/action_bar_subtitle_top_margin"
android:singleLine="true"
android:ellipsize="end"
android:visibility="gone" />
</LinearLayout>
不同 API 版本的布局可以而且确实有所不同。一般来说,ID 会匹配,但我不能说这对每个布局都是正确的,而且永远都是这样,所以如果您打算使用像这样的特定内置资源以防万一,最好快速查看每个布局的差异。
为此,您必须获取源代码的单独副本以查看每个 API 版本。
这可以通过以下方式完成:
手动方法:自己调用位于 SDK 中的 SDK 版本:
.../android-sdk/tools/android
您显然只需要获取您支持的版本,正如我上面提到的,每个版本都有自己的文件夹,以其单个 int 版本号命名。例如:
.../android-sdk/platforms/android-19/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-15/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-11/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-10/data/res/layout/???
呃哦! API-10 没有布局。如果您想在该版本的 API 中使用 action_bar_title_item.xml 布局,您可以包含并使用 Sherlock ActionBar,或者复制或修改 v11 布局以:
.../YourApp/res/layout-v10/action_bar_title_item.xml (Eclipse)
.../YourApp/app/src/main/res/layout-v10/action_bar_title_item.xml (Android Studio)
你来电。如果您只想自定义布局,也可以这样做。将 android 内置版本复制到您自己的布局文件夹并调整您的心脏内容。请注意您将文件放置在哪些文件夹中,以便它们覆盖所需的正确版本。
关于android - 在android中获取ActionBar TextView(标题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24344007/
我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象
是否可以在应用程序中包含的gem代码中知道应用程序的Rails文件系统根目录?这是gem来源的示例:moduleMyGemdefself.included(base)putsRails.root#returnnilendendActionController::Base.send:include,MyGem谢谢,抱歉我的英语不好 最佳答案 我发现解决类似问题的解决方案是使用railtie初始化程序包含我的模块。所以,在你的/lib/mygem/railtie.rbmoduleMyGemclassRailtie使用此代码,您的模块将在
我有一个应用程序可以读取文件的内容并为其编制索引。我将它们存储在磁盘本身中,但现在我使用的是AmazonS3,因此以下方法不再适用。事情是这样的:defperform(docId)@document=Document.find(docId)if@document.file?#Youshould'tcreateanewversion@document.versionlessdo|doc|@document.file_content=Cloudoc::Extractor.new.extract(@document.file.file)@document.saveendendend@docu