java 在 Android 中设置 TableRow 的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1144851/
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
Set style on TableRow in Android
提问by stevedbrown
Using the Android SDK, there doesn't seem to be any way to set the style on a table row. I want to do the equivalent of:
使用 Android SDK,似乎没有任何方法可以在表格行上设置样式。我想做相当于:
<TableRow
style='@style/TableRow_classic' >
in code. I would like something like:
在代码中。我想要类似的东西:
TableRow row = new TableRow(this);
row.setStyle(R.style.TableRow_classic);
Does anyone know how to find this?
有谁知道如何找到这个?
回答by Jim Geurts
The best solution I've found to accomplish this is to create a "partial" layout representing the table row, with a style defined:
我发现实现此目的的最佳解决方案是创建一个表示表格行的“部分”布局,并定义样式:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/tr_withborder" />
Then inflate it in your code, to use dynamically:
然后在您的代码中对其进行充气,以动态使用:
TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.partial_table_row, null);
While not ideal, it does work for me
虽然不理想,但它对我有用
回答by stevedbrown
As far as I can tell, there is no setStyle method. You could go through and set each individual property, but that defeats the purpose of separating your View from your controller logic. Bad.
据我所知,没有 setStyle 方法。您可以检查并设置每个单独的属性,但这违背了将 View 与控制器逻辑分离的目的。坏的。
Has anyone figured out how to dynamically assign styles?
有没有人想出如何动态分配样式?
回答by verylongword
I am trying myself to set style properties obtained from my Theme in code.
我正在尝试在代码中设置从我的主题获得的样式属性。
My reason is that I want to use a style from my own Theme, BUT my User Interface Layout is entirely generated in code( using a custom layout builder), without defining any widgets in XML. So I cannot set a style in the XML layout of my widget – there isn't any XML layout.
我的原因是我想使用我自己主题中的样式,但我的用户界面布局完全是在代码中生成的(使用自定义布局构建器),而没有在 XML 中定义任何小部件。所以我不能在我的小部件的 XML 布局中设置样式——没有任何 XML 布局。
I am thinking that I will be able to set this style in the code of my widget by using
我想我将能够通过使用在我的小部件的代码中设置这种样式
TypedArray a = context.obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
Here it seems (to me) that
这似乎(对我来说)
AttributeSet set = null;because this is what the XML inflater would have provided.int[] attrs = R.styleable.MyWidget;defines what attributes I want to look at.int defStyleAttr = myWidgetStyle;which is a reference, defined in my Theme, to a style for MyWidget. These are both defined in XML files in res/values. “myWidgetStyle” follows the pattern of name the android developers have used in their code.defStyleRes = 0;I am hoping that I don't need to think about this.
AttributeSet set = null;因为这是 XML inflater 会提供的。int[] attrs = R.styleable.MyWidget;定义我想要查看的属性。int defStyleAttr = myWidgetStyle;这是在我的主题中定义的对 MyWidget 样式的引用。这些都在 res/values 中的 XML 文件中定义。“myWidgetStyle”遵循 android 开发人员在其代码中使用的名称模式。defStyleRes = 0;我希望我不需要考虑这个。
Then to get any property , such as a background color,
然后要获取任何属性,例如背景颜色,
Color color = a.getColor(R.styleable.MyWidget_background, R.color.my_default);
a.recycle();
This does seem to work –so far anyway.
这似乎确实有效 - 无论如何。
It seems that the android build system conveniently generates the correct index to use in a.getColor, and names it R.styleable.MyWidget_background. I didn't make this name, so Android must have done it using my XML for my styleable MyWidget.
android 构建系统似乎很方便地生成了在 a.getColor 中使用的正确索引,并将其命名为R.styleable.MyWidget_background. 我没有取这个名字,所以 Android 一定是使用我的 XML 来为我的样式化 MyWidget 做的。
I expect one can look up the correct index by searching the TypedArray for the required attribute , but that would be inefficient and the TypedArray looks like an unpleasant contraption to deal with. I would use a very long stick to poke it!
我希望人们可以通过在 TypedArray 中搜索所需的属性来查找正确的索引,但这效率低下,而且 TypedArray 看起来像是一个令人不快的装置。我会用一根很长的棍子戳它!
Don
大学教师

