gpt4 book ai didi

android - 神秘 Activity 不符合应用主题

转载 作者:太空狗 更新时间:2023-10-29 13:16:16 27 4
gpt4 key购买 nike

在 Android 平台上开发了几个月之后,我仍然有一个悬而未决的问题。很久以前,我注意到我有一个 Activity 不符合应用程序主题的其余部分。这意味着默认情况下,Activity 的字体颜色是白色(不是黑色),Checkboxes 没有被选中时是不显示的,你根本看不到它们。非常奇特的行为。最近,我更改了主题的 colorAccent 属性,将所有强调色更改为橙​​色(包括复选框),但此 Activity 保持不变。有没有人遇到过这种情况?

我把这个Activity做了一次fragment,然后就神奇的符合了主题。切换回 Activity 后,即使做了一个全新的 Activity 来承载逻辑,也没有任何改变。这是 Activity 布局:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
>

<android.support.design.widget.AppBarLayout android:id="@+id/appbar"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<me.core.utility.TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="24sp"
android:id="@+id/title_bar"
android:textColor="@android:color/white"
android:text="Edit"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/appbar"
android:id="@+id/listview"
android:scrollbars="none"
android:layout_above="@+id/buttons_layout"
android:layout_gravity="center_horizontal|top"
/>

<LinearLayout
android:weightSum="2"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="@android:color/transparent"
android:id="@+id/buttons_layout"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL"
android:layout_weight="1"
android:layout_margin="@dimen/activity_vertical_margin"
android:id="@+id/cancel_editing_button"
android:background="@drawable/custom_button_logout"
android:textColor="@android:color/white"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
android:layout_weight="1"
android:layout_margin="@dimen/activity_vertical_margin"
android:id="@+id/done_editing_button"
android:background="@drawable/custom_button_logout"
android:textColor="@android:color/white"
/>

</LinearLayout>

</RelativeLayout>

下面是这一切的图片:

enter image description here

有没有人知道为什么这个 Activity 变得流氓?

编辑:添加 list

<application
tools:replace="android:icon"
android:allowBackup="true"
android:icon="@mipmap/ic_trans2"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:name=".utility.AppInit"
>

<!-- [START gcm_receiver] -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />

<category android:name="software.gcm" />

<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->

<!-- [START gcm_listener] -->
<service
android:name=".gcm.GcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->


<!-- [START instanceID_listener] -->
<service
android:name=".gcm.InstanceIDListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END instanceID_listener] -->


<!-- [START registration_service] -->
<service
android:name=".gcm.RegistrationIntentService"
android:exported="false" >
</service>
<!-- [END registration_service] -->


<!-- [START application activities] -->
<activity
android:name=".userinterface.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".session.InitSessionActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".userinterface.ProfileActivity"
android:label="@string/title_activity_profile"
android:screenOrientation="portrait"
android:parentActivityName=".userinterface.MainActivity"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="me.core.userinterface.MainActivity" />
</activity>
<activity
android:name=".userinterface.AddBuddyActivity"
android:label="Add Buddy"
android:screenOrientation="portrait"
android:parentActivityName=".userinterface.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".userinterface.MainActivity" />
</activity>
<activity
android:name=".userinterface.ModifyActivity"
android:label=""
android:screenOrientation="portrait"
android:parentActivityName=".userinterface.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".userinterface.MainActivity" />
</activity>
<activity
android:name=".userinterface.BuddyRequestsActivity"
android:label=""
android:screenOrientation="portrait"
android:parentActivityName=".userinterface.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".userinterface.MainActivity" />
</activity>

<activity
android:name=".userinterface.ThreadDetailsActivity"
android:label="@string/title_activity_messages"
android:screenOrientation="portrait"
android:parentActivityName=".userinterface.MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".userinterface.MainActivity" />
</activity>

<!-- [END application activities] -->
</application>

编辑:添加源

public class ModifyActivity extends AppCompatActivity implements DeleteConfirmationDialog.DeleteConfirmationListener {
private static String LOG_TAG = ModifyActivity.class.getCanonicalName();
private DatabaseHelper databaseHelper;
private BusProvider busProvider;

/** UI references*/
private ProgressDialog progressDialog;
TextView title;

private Obj selectedObj; // The obj to be edited
private ModifyActivityListAdapter modifyActivityListAdapter;

private ArrayList<Buddy> buddiesToAdd; // Buddies to be added
private ArrayList<Buddy> buddiesToRemove; // Buddies to be removed

// Allows these two processes to keep track of one another, and go back to buddies activity when both are done
private boolean additionDone;
private boolean deletionDone;
private boolean actionTaken;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify);
busProvider = BusProvider.getInstance();
databaseHelper = DatabaseHelper.getInstance(getApplicationContext());
title = (TextView)findViewById(R.id.title_bar);
int id = Integer.valueOf(getIntent().getStringExtra(ConstantValues.IntentArguments.ARG_ID));
// The object to be edited
selectedObj = databaseHelper.selectObjWithId(id);
// if (selectedObj!=null) {
// title.setText(selectedObj.getName());
// }

// Get all members of chosen obj
ArrayList<Member> membersOfSelectedObj = databaseHelper.selectMembersOfObj(id);

//Instantiate
ListView editListView = (ListView)findViewById(R.id.edit_listview);
// Create adapter
modifyActivityListAdapter = new ModifyActivityListAdapter(getApplicationContext(), membersOfSelectedObj);
// Attach adapter
editListView.setAdapter(modifyActivityListAdapter);

Button doneButton = (Button)findViewById(R.id.done_editing_button);
Button cancelButton = (Button)findViewById(R.id.cancel_editing_button);
ImageButton deleteButton = (ImageButton)findViewById(R.id.delete_button);
ImageButton renameButton = (ImageButton)findViewById(R.id.rename_button);

doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
analyzeUserInput(modifyActivityListAdapter.getAllBuddies());
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createDeleteConfirmDialog();
}
});
renameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createRenameConfirmDialog();
}
});

}

编辑:发布列表项的代码,这是真正变色的东西。复选框应为橙色,字体为黑色,但它们完全不可见(白色),因此我需要手动更改文本颜色。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:minHeight="?android:attr/listPreferredItemHeight"
tools:context=".userinterface.ModifyActivity"
>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:text="howdy"
android:textColor="@android:color/black"
android:textSize="20sp"
android:layout_centerVertical="true"
android:id="@+id/edit_list_item_textview"/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="@color/lb_orange"
android:layout_centerVertical="true"
android:id="@+id/edit_list_item_checkbox"
android:focusable="false"
android:layout_alignParentRight="true"
/>


</RelativeLayout>

最佳答案

这是因为你如何构建你的 ModifyActivityListAdapter .您正在为其提供一个应用程序上下文,适配器使用它来扩充其项目 View 。尝试传入 Activity 上下文。

// Create adapter
modifyActivityListAdapter = new ModifyActivityListAdapter(this, membersOfSelectedObj);

默认情况下,应用程序上下文不包含主题信息。因此,用它膨胀的 View 不会有您的自定义主题。

当您指定 android:theme 时在你的 list 中 <application>元素,它不应用于 Application 对象,而是应用于 Activity 级别。该属性只是为了方便指定一个应用于所有主题的主题 <activity>元素。您可以使用 Context.setTheme(..)将主题应用于您的应用程序上下文对象,但不推荐这样做。相反,无论何时使用 View ,您都应该使用 Activity 上下文。

更多信息请查看Dave Smith's detailed article about the Context class .它是从 2013 年开始的,但从那以后 Contexts 并没有真正改变太多。

关于android - 神秘 Activity 不符合应用主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408338/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com