小编Kyr*_*iog的帖子

重置搜索窗口小部件(SearchView)值

我有2个活动:第一个,HomepageActiviy有一个搜索小部件,使用另一个活动搜索数据,SearchActivity.

我想要做的是当我回到SearchActiviyHomepageActivity,搜索小部件折叠并带有空文本.

我试图做以下事情:

public class HomepageActivity extends Activity {
    @TargetApi(11)
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.projectslist, menu);

        if(Build.VERSION.SDK_INT >= 11) {
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) menu.findItem(R.id.homepage_search).getActionView();
            ComponentName component = new ComponentName(this, SearchActivity.class);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(component));
            searchView.setIconifiedByDefault(true);
            searchView.setQuery("", false);

        }

        return super.onCreateOptionsMenu(menu);
    }

    […]

    @TargetApi(11)
    @Override
    protected void onRestart() {
        super.onRestart();
        if(Build.VERSION.SDK_INT >= 11)
            invalidateOptionsMenu();
        launchAsynchronousImageDownload();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果窗口小部件显示为已折叠,则窗口小部件中的文本仍会记住搜索到的文本(在重新打开窗口小部件后).如何重置小部件的文本?

谢谢你的帮助!;)

android android-widget

24
推荐指数
4
解决办法
3万
查看次数

Android - 黑色背景变成灰色

我明确地为我的LinearLayout的背景定义了一种黑色(在我的XML中使用@android:color/black),但有时(通常在屏幕旋转后),黑色变成灰色(或透明的黑色) ).

此问题出现在许多设备上:模拟器,Acer Liquid(Android 2.2)和Galaxy Nexus(Android 4.1).

截图:Buggy视图 // 没有错误的视图

这是我的XML和活动代码:

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:minHeight="150dp">

            <ImageView
                android:id="@+id/projectview_description_image"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:contentDescription="@string/projectview_description_image_description"
                android:scaleType="centerCrop"
                android:src="@drawable/project_nophoto" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:orientation="vertical"
                android:id="@+id/projectview_description_overlay"
                android:paddingTop="5dp"
                android:paddingLeft="10dp"
                android:paddingBottom="5dp"
                android:background="@android:color/black"
                android:height="35dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/projectview_description_title"
                    android:textAllCaps="true"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="bold"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/projectview_description_baseline"
                    android:textColor="@android:color/white"
                    android:textStyle="italic" />

            </LinearLayout>

        </RelativeLayout>

        <!-- This LinearLayout background is buggy -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal"
            android:background="@android:color/black">

            <ImageView
                android:layout_width="175dp"
                android:layout_height="25dp"
                android:layout_gravity="center_vertical" …
Run Code Online (Sandbox Code Playgroud)

android android-layout

6
推荐指数
1
解决办法
3208
查看次数

主题更改不适用于<4.0

以编程方式设置"主题切换器"时遇到一些困难.我想从app(白色(Theme.Light.NoTitleBar)和Dark(Theme.Black.NoTitleBar)之间)切换主题,我的工作是:我设置了一个SharedPreference:

final String PREFS_NAME = "MyPrefsFile";
    final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    final SharedPreferences.Editor editor = settings.edit();
Run Code Online (Sandbox Code Playgroud)

而且我有两个按钮来切换主题(第二个几乎相同)

Button ThemeWhite = (Button) findViewById(R.id.ThemeWhite);
    ThemeWhite.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            editor.putBoolean("Theme", false);
              editor.commit();
              System.exit(2);
        }
    });
Run Code Online (Sandbox Code Playgroud)

在每个活动的乞讨中,我检查SharedPreference

boolean theme = settings.getBoolean("Theme", false);
    if(theme){
        this.setTheme(R.style.Theme_NoBarBlack);
    } else{
        this.setTheme(R.style.Theme_NoBar);
    }

    setContentView(R.layout.aplikacja);
Run Code Online (Sandbox Code Playgroud)

我在文件styles.xml中定义文件夹值中的主题:

<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.Light.NoTitleBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.NoTitleBar" />
Run Code Online (Sandbox Code Playgroud)

在值-v11中:

<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.Holo.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.Holo.NoActionBar" />
Run Code Online (Sandbox Code Playgroud)

在值-v14中:

<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar" />
<style name="Theme.NoBarBlack" …
Run Code Online (Sandbox Code Playgroud)

android themes styles android-layout android-theme

5
推荐指数
1
解决办法
2343
查看次数