神秘活动与应用主题不符

3
在Android平台开发数月后,我仍有一个未解决的问题。很久以前我就注意到只有一个Activity与应用程序主题不符合。这意味着默认情况下,Activity的字体颜色是白色(而不是黑色),当复选框未被选中时,它们不会显示出来,你根本看不到它们。非常奇怪的行为。最近,我改变了我的主题的colorAccent属性,将所有强调颜色都改成了橙色(包括复选框),但是这个Activity仍然没有改变。有人经历过这种情况吗?
我曾经将这个Activity制作成片段,然后它神奇地符合了主题。在切换回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>

这是所有内容的示意图:

在这里输入图片描述

有人知道为什么这个Activity变得难以控制吗?

编辑:添加了清单文件。

<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>

1
你在manifest文件中如何声明这个Activity,是用android:theme属性声明的吗? - Satyen Udeshi
是的,它已经被声明并且清单主题已经设置。 - Brandon
你能添加你的“清单”吗? - Satyen Udeshi
我发布了onCreateView代码,实际上这是唯一直接涉及到活动的东西。 - Brandon
1
不要解释内容。 - Dhaval Parmar
显示剩余10条评论
3个回答

3
由于您构建了ModifyActivityListAdapter的方式,导致出现问题。您给它提供了一个应用上下文,这个适配器用来填充其项目视图。尝试传入活动上下文。
// Create adapter
modifyActivityListAdapter = new ModifyActivityListAdapter(this, membersOfSelectedObj);

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

当您在清单文件的<application>元素中指定android:theme时,它不会应用于Application对象,而是应用于活动级别。该属性只是为了方便指定应该应用于所有<activity>元素的一个主题。您可以使用Context.setTheme(..)将主题应用于应用程序上下文对象,但不建议这样做。相反,每当您使用视图时,应使用活动上下文。

有关更多信息,请查看Dave Smith关于Context类的详细文章。它是来自2013年的,但是自那以来Contexts并没有发生太大变化。


所有我的活动都继承自AppCompatActivity。 - Brandon
好的。您为什么要在ModifyActivityListAdapter构造函数中提供应用程序上下文而不是活动上下文呢? - Jozua
不,应该使用活动上下文。你说得对!但你认为这会有所区别吗?我需要了解更多关于何时使用哪种上下文的信息。但据我所知,如果你传递给它的是与整个应用程序相关的内容,则应该只使用应用程序上下文,否则应该使用活动上下文。 - Brandon
不错的发现Jozua。我认为你可能是对的。 - C B J
非常感谢@Jozua。那真是一个了不起的发现,我从未想过使用应用程序上下文与活动上下文可能会造成如此多的混乱。问题现在已经解决,我授予您赏金。 - Brandon

0

在被用于setContentView的xml布局文件中,进入可视化设计部分并查看该窗口顶部的标题。您可以通过单击活动名称选项卡左侧的按钮来设置主题。


-1

你有检查过代码吗?比如说,在你的ListView实现中,可能存在ViewHolder设计模式的问题。因为适配器代码还没有发布。


这种情况以前发生过,但在这个实例中并非如此。没有设置自定义背景或任何修改列表项视图的内容... - Brandon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接