我对这个话题有一些疑问。
我只使用 android 为我们提供的 API 的夜间/白天主题,您可以为两者设置主题。然后它适用于 setNightMode() 和类似的东西。然后我所做的是在视图背景中设置一种颜色,当设置为夜间主题时它会发生变化
所以我想知道我是否像这样创建 3 个主题(浅色/深色/蓝色)(我不想重复代码,所以我将显示 1 个主题):
<style name="Theme.Blue" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/green</item>
<item name="colorPrimaryVariant">@color/greentwo</item>
<item name="colorOnPrimary">@color/reed</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
Run Code Online (Sandbox Code Playgroud)
然后当我进入活动/片段时:
我检查用户设置的首选主题是什么,例如看到的主题是Theme.Blue
因此,对于这个主题,我想加载一些绿色文本和其他红色文本,所以我的想法是以编程方式设置主题。TextView.setAppareance(Theme style)这似乎是一项非常艰苦的工作。
同样的情况也适用于背景,其中存在相同颜色的较高和较低色调的对比等等。
我是否必须继承任何主题,将其设置在 xml 的顶部或将其 1 by 1 设置到每个视图或我不知道的任何内容?
那么我该怎么做呢,推荐的方法是什么?
我不太明白 colorPrimary、colorPrimaryVariant 之类的东西去哪里了。例如,当创建警报对话框时,按钮设置为绿色!
关于管理当前用户首选项主题不是问题,问题在于将其应用到所有视图的样式。
我遇到了与其他 20 个主题几乎相同的问题,这些主题指向RuntimeException: Parcel android.os.Parcel@72a6e36: Unmarshalling unknown type code,但偏移量不同(500):
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@72a6e36: Unmarshalling unknown type code 2131365270 at offset 500
at android.os.Parcel.readValue(Parcel.java:2443)
at android.os.Parcel.readSparseArrayInternal(Parcel.java:2798)
at android.os.Parcel.readSparseArray(Parcel.java:2067)
at android.os.Parcel.readValue(Parcel.java:2421)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2717)
at android.os.BaseBundle.unparcel(BaseBundle.java:269)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:910)
at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2129)
at android.app.Activity.onRestoreInstanceState(Activity.java:1047)
at android.app.Activity.performRestoreInstanceState(Activity.java:1002)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
Run Code Online (Sandbox Code Playgroud)
尽管如此,这些主题并没有为我的案例提供解决方案。
我的问题是在将 2 个类从 Java 转换为 Kotlin 后开始出现的,这些类继承自ViewGroup并且View仅在应用程序的混淆版本中。
我的混淆器配置有一个规则:
-keepclassmembers class * implements android.os.Parcelable {
static ** CREATOR; …Run Code Online (Sandbox Code Playgroud) 其中(最好是在可在线浏览的源存储库中)是为PreferenceActivity?定义的布局?我想复制标题的不同样式等.
我看到android.preference.PreferenceActivity有一个引用com.android.internal.R.layout.preference_list_content,但是我没有在源代码中看到通过SDK下载的R目录 [android-sdk]\sources\android-15\com\android\internal.
我假设源树中的其他Android布局都在附近.

如图所示,当用户触摸/点击EditText时,我需要类似的功能.键盘打开时,视图应覆盖半透明覆盖.实现这个的最佳方法是什么?
我在FrameLayout中查看了我的视图.
我想在执行后台任务之前操作异步任务中的特定视图但不确定如何将所需参数传递给方法.当我做这样的事情时,我得到一个空指针
class CalcFib extends AsyncTask<Integer, Void, Long> {
private View v;
private int index;
public CalcFib(int i){
this.index = i;
}
@Override
protected void onPreExecute() {
//do something with this.index
}
@Override
protected Long doInBackground(Integer... params) {
long t = 1234;
return t;
}
@Override
protected void onPostExecute(Long result) {
}
}
Run Code Online (Sandbox Code Playgroud) 假设一个Activity根视图永远是一个ViewGroup?是安全的吗?我有一个Activity显示各种Fragments.我可以跳过为其创建布局XML Activity并直接将Fragments 添加到其根视图中吗?
我想从Web服务动态创建单选按钮,复选框,微调器.以下是我的回复.
{
"getSurvey": {
"ErrorCode": "0",
"Result": "Success",
"Response": [
{
"SurveyId": "91",
"SurveyName": "Sample Survey",
"Questions": [
{
"QuestionId": "553",
"QuestionText": "<p style=\"margin: 0px; text-align: left;\">Are you satisfied with Cement Quality?</p>",
"Type": "RadioButtonList",
"Mandatory": "Y",
"OptionValue": [
{
"AnswerId": "2220",
"AnswerValue": "Excellent"
},
{
"AnswerId": "2221",
"AnswerValue": "Good"
},
{
"AnswerId": "2222",
"AnswerValue": "Satisfactory"
},
{
"AnswerId": "2223",
"AnswerValue": "Poor"
}
]
},
{
"QuestionId": "554",
"QuestionText": "How will you rate Cement? (0= Lowest and 7= Highest) ",
"Type": "DropDownList", …Run Code Online (Sandbox Code Playgroud) 我正在尝试完全在屏幕外创建和渲染视图。首先,我尝试过(来自http://arpitonline.com/2012/07/17/capturing-bitmaps-of-views-in-android/):
Bitmap result = Bitmap.createBitmap(1080, 1080, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
view.layout(0, 0, 1080, 1080);
view.draw(c);
return result;
Run Code Online (Sandbox Code Playgroud)
它仅呈现我视图的背景色。大小和背景颜色正确,但是内容完全空白。然后我偶然发现了这一点:
view.layout(0, 0, 1080, 1080);
view.invalidate();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Bitmap result = bmp.copy(Bitmap.Config.ARGB_8888, false);
view.destroyDrawingCache();
return result;
Run Code Online (Sandbox Code Playgroud)
但同样,它会产生相同的结果:正确的大小和背景颜色以及空白内容。我的视图中有很多视图,我在它们上设置了许多属性,没有空指针等,所有设置都正确。但是他们只是没有被吸引。为什么会这样?
更新:正如psking建议的那样,我已将绘图逻辑移至,View#post但它仍然相同(尽管触发完成需要花费更长的时间,这可能是视图实际执行某项操作的线索)。尝试了两种方法:
(完成是我需要位图的函数)
final Bitmap result = Bitmap.createBitmap(1080, 1080, Bitmap.Config.ARGB_8888);
final Canvas c = new Canvas(result);
view.layout(0, 0, 1080, 1080);
view.post(new Runnable() {
@Override
public void run() {
view.draw(c);
completion.call(result);
}
});
Run Code Online (Sandbox Code Playgroud)
和
view.layout(0, 0, 1080, …Run Code Online (Sandbox Code Playgroud) android view off-screen android-view android-4.0-ice-cream-sandwich
我正在使用具有5个项目,4个片段和1个活动的BottomNavigationView,如下图所示
当用户点击任何片段,我希望它能够正常行动变得clicked和checked,但是当“+”项目被点击,我打开一个活动,但我不希望它是checked,所以我想这是clickable因此它可以打开活动,但我不想成为checked,因为当用户从活动中返回时,即使它是错误选择的项目,也会看到它被选中。我怎样才能做到这一点?
这是我的代码:
bottomNavigationView = findViewById(R.id.bottom_nav_view);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
Run Code Online (Sandbox Code Playgroud)
。
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.home_nav_menu:
fragment = new HomeFragment();
break;
case R.id.inbox_nav_menu:
fragment = new InboxFragment();
break;
case R.id.add_nav_menu:
Intent intent = new Intent(this, AddActivity.class);
startActivity(intent);
return true;
case R.id.history_nav_menu:
fragment = new HistoryFragment();
break;
case R.id.profile_nav_menu:
fragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_place_holder, fragment).commit();
return true; …Run Code Online (Sandbox Code Playgroud) android android-layout android-menu android-view bottomnavigationview
android ×10
android-view ×10
android-4.0-ice-cream-sandwich ×1
android-menu ×1
android-xml ×1
java ×1
json ×1
kotlin ×1
off-screen ×1
parcelable ×1
styles ×1
view ×1