一般来说,您如何找到要覆盖的主题属性以改变任何 UI 元素的外观?
目前我依赖于浏览框架源文件:values.xml 中的主题定义(通常是支持库变体)、attrs.xml 中的属性定义和 R.styleable 类文档。
但这完全是碰运气。这不仅非常耗时,而且有时我会完全错过,例如,我一直在尝试找出如何更改 DatePickerDialog 的“确定”和“取消”按钮中的文本样式,但没有成功。随意使用它作为示例,但如果您这样做,请概述您的发现过程。我正在寻找的答案是如何发现任何 UI 元素的应用样式,
或者只是没有确定性的方法来找出答案?你只需要知道吗?
最佳答案
寻找如何在 Android 上更改小部件的样式一直很麻烦。例如,DatePickerDialog 有不同的 Holo 和 Material 设计风格。因此,对话框的样式可能取决于 SDK 值或您是否使用 AppCompat 库。文档也很少。
如果像Hierarchy Viewer这样的工具就好了显示小部件的属性和当前主题。但是,我还没有遇到过这样的工具。
我们可以在创建 View 后获取当前的主题和属性。以下是我在 DatePickerDialog 上编写和测试的几个方法,用于查找正在使用的样式:
从上下文中获取当前主题:
static String getThemeNameFromContext(Context context) {
Resources.Theme theme = context.getTheme();
String themeName;
try {
Field field = theme.getClass().getDeclaredField("mThemeResId");
if (!field.isAccessible()) {
field.setAccessible(true);
}
int themeResId = field.getInt(theme);
themeName = context.getResources().getResourceEntryName(themeResId);
} catch (Exception e) {
// If we are here then the context is most likely the application context.
// The theme for an application context is always "Theme.DeviceDefault"
themeName = "Theme.DeviceDefault";
}
return themeName;
}
获取属性的名称/值:
static String getResourceName(Context context, int attribute) {
TypedArray typedArray = context.obtainStyledAttributes(new int[]{attribute});
try {
int resourceId = typedArray.getResourceId(0, 0);
return context.getResources().getResourceEntryName(resourceId);
} finally {
typedArray.recycle();
}
}
在下面的示例中,我创建了一个 DatePickerDialog 并使用上述方法获取了对话框使用的主题和属性值以及对话框的肯定按钮:
// Create the DatePickerDialog
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
}, 2015, Calendar.SEPTEMBER, 9);
// Show the dialog
datePickerDialog.show();
// Get the positive button from the dialog:
Button positiveButton = datePickerDialog.getButton(DatePickerDialog.BUTTON_POSITIVE);
// Get the theme used by this dialog
String theme = getThemeNameFromContext(datePickerDialog.getContext());
// Get the date picker style used by the dialog
String datePickerStyle = getResourceName(datePickerDialog.getContext(), android.R.attr.datePickerStyle);
// Get the style of the positive button:
String buttonStyle = getResourceName(positiveButton.getContext(), android.R.attr.buttonStyle);
Log.i("LOGTAG", "Theme: " + theme);
Log.i("LOGTAG", "datePickerStyle: " + positiveButton);
Log.i("LOGTAG", "buttonStyle: " + buttonStyle);
在我的测试项目中,我得到了 theme 的这些值, datePickerStyle , 和 buttonStyle :
Theme: ThemeOverlay.Material.Dialog
datePickerStyle: Widget.Material.Light.DatePicker
buttonStyle: Widget.Material.Light.Button
这有点帮助,但我们仍然没有改变正面和负面按钮的样式。如果我们查看 source for DatePickerDialog我们可以看到它扩展了 AlertDialog。这会使事情变得更难,因为您需要在主题中设置自定义样式,这会影响所有 AlertDialog 按钮。如果您需要有关如何更改 buttonStyle 的示例,请发表评论。
更好的方法是在对话框可见后设置正负按钮的样式。例如,在您的 DialogFragment 中,您可以将以下代码放在 onStart() 中设置按钮样式:
@Override
public void onStart() {
super.onStart();
DatePickerDialog dialog = (DatePickerDialog) getDialog();
Button btnPos = dialog.getButton(DatePickerDialog.BUTTON_POSITIVE);
Button btnNeg = dialog.getButton(DatePickerDialog.BUTTON_NEGATIVE);
/* customize the buttons here */
btnPos.setText("CUSTOM");
btnPos.setTextAppearance(android.R.style.TextAppearance_Large);
btnNeg.setTextColor(Color.RED);
}
结论:
您可以在其他 View 上使用上述方法来找到适用于该 View 的样式。在 Android 上设计小部件的样式仍然是一件痛苦的事情。您可能需要不时地研究源代码和 XML。
关于android - 有没有一种通用的方法可以找到适用于 View 的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32373540/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html