相关疑难解决方法(0)

以编程方式更改活动的主题

在特殊情况下,我需要从我的活动中删除对话框主题,但它似乎不起作用.这是一个例子

第一项活动:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
Run Code Online (Sandbox Code Playgroud)

第二项活动:

public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme);
    setContentView(R.layout.activity_second);
}
Run Code Online (Sandbox Code Playgroud)

清单摘录:

 <activity android:name="SecondActivity" android:theme="@android:style/Theme.Dialog"></activity>
Run Code Online (Sandbox Code Playgroud)

当我运行它仍然是对话主题.

API10

谢谢.

android

104
推荐指数
5
解决办法
9万
查看次数

Android:将不同的主题应用于一个活动的片段

我想做的事:

我希望我的MainActivity的每个片段使用不同的主题,以便ActionBar具有不同的背景颜色,具体取决于可见的片段.

情况:

我创建了一个使用Tabs + Swipe Navigation的MainActivity.我添加了7个标签(= 7个片段).我创建了一个主题,应该只应用于第一个片段(fragment_main_1).

这里的主题:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Blue" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/Blue.ActionBarStyle</item>
</style>

<style name="Blue.ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/Blue.ActionBar.TitleTextStyle</item>
    <item name="android:background">#33B5E5</item>
</style>

<style name="Blue.ActionBar.TitleTextStyle" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#FFFFFF</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)

在创建了6个主题之后,应该可以在ActionBar自动更改其背景颜色的同时滑动选项卡.

什么行不通:

将这些行(我在stackoverflow上找到)添加到Fragment1.java:

// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.Blue);

// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.fragment_main_1,container, false);
Run Code Online (Sandbox Code Playgroud)

我希望你能帮帮我:)谢谢.

android android-layout

21
推荐指数
1
解决办法
1万
查看次数

Android为片段设置透明背景

在我的应用程序中,我有单个活动和所有其他片段

我正在设置style.xml的活动背景,如下所示

<item name="android:windowBackground">@color/very_light_gray</item>
Run Code Online (Sandbox Code Playgroud)

现在只有一个特殊的片段,我想设置背景透明,我无法做到尝试下面的片段代码片段对我来说不起作用

  @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);

// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.yourLayout, container, false);
}
Run Code Online (Sandbox Code Playgroud)

知道怎么做吗?

android android-theme android-fragments android-activity android-support-library

13
推荐指数
2
解决办法
7455
查看次数

主题不适用于Fragment

我正在尝试在片段上应用特定主题.但出于某种原因,它并没有发生.任何人都可以指出我的代码中的错误吗?或更好的解决方案

styles.xml中的主题:

<!-- Theme for trans actionbar -->
<style name="TransTheme" parent="@style/AppTheme">
    <item name="android:actionBarStyle">@style/TransActionbar</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="actionBarStyle">@style/TransActionbar</item>
    <item name="windowActionBarOverlay">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在哪里@style/TransActionbar:

<!-- Actionbar style for trans theme -->
<style name="TransActionbar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/first_trans_actionbar</item>
    <item name="background">@color/first_trans_actionbar</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我想如何在onCreateView()方法中的片段中应用主题:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //set theme
    final Context contextWrapper = new ContextThemeWrapper(getActivity(), R.style.TransTheme);
    LayoutInflater localInflater = inflater.cloneInContext(contextWrapper);
    contentView = localInflater.inflate(R.layout.fragment_movie_detail,
            container, false);
    return contentView:
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,Fragment保留了旧主题..

编辑

所以基本上问题归结为:如何在一个活动的一个片段中使Actionbar透明(和叠加模式)?

android android-fragments

9
推荐指数
2
解决办法
8164
查看次数

将自定义主题应用于PreferenceFragment

我有左窗口和右边片段的多窗格视图.在右侧片段上启动PreferenceFragment.问题是片段看起来完全失真没有任何风格.有没有办法仅将主题应用于PreferenceFragment?

我尝试了这个,但它没有用

我的代码

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.AppTheme_PreferenceTheme);

    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);


    View view = super.onCreateView(localInflater, container, savedInstanceState);

    return view;

}

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 

    addPreferencesFromResource(R.xml.app_settings_preference_layout);
}
Run Code Online (Sandbox Code Playgroud)

我认为解决方案不起作用,因为我已经在onCreate中夸大了首选项布局.有没有办法在不使用addPreferencesFromResource方法而只使用LayoutInflater服务的情况下对首选项布局进行膨胀?

android android-preferences android-fragments

7
推荐指数
1
解决办法
2381
查看次数

Fragment中的自定义ListView不遵循父主题

我目前在使用Holo.Light主题的自定义ListView适配器时遇到问题.在活动和片段中,任何TextView都显示在主题的常规颜色(textColorPrimary)中.但是,自定义ListAdapter中的任何文本都使用textColorPrimary默认的Holo主题,从而有效地使文本不可读.

以下是我应用主菜单中的示例:


list_main_menu.xml - ListAdapter的行布局

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

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_height="48dip"
        android:layout_width="48dip"
        android:src="@drawable/ic_launcher"
        />

    <TextView 
        android:id="@+id/txtFirstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imgIcon"
        android:text="Line 1"
        android:textSize="12pt"
        />

    <TextView
        android:id="@+id/txtSecondLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imgIcon"
        android:layout_below="@id/txtFirstLine"
        android:text="Line 2"
        />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

注意:我目前不得不android:textColor="?android:attr/textColorPrimaryInverse"用来使文本可读.


fragment_main_menu.xml - 主菜单片段.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical" >
    <TextView
        android:id="@+id/txtWelcome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Username"
        android:textSize="18sp"
        />
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />     
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

AndroidManifext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.michaeldodd.treasurehunter" …
Run Code Online (Sandbox Code Playgroud)

android android-listview android-theme

4
推荐指数
1
解决办法
4616
查看次数

Android setTheme() 不改变 ActionBar 和 Status Bar

我有一个ActionBarActivity其中有导航抽屉的片段。当按下导航抽屉中的某个项目时,它会启动相应的片段。在每个片段中,该onCreate()方法如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setTheme(R.style.AppTheme);

    setHasOptionsMenu(true);
}
Run Code Online (Sandbox Code Playgroud)

然而,每个片段所应用的主题都不同。

我注意到该方法似乎没有更改我引用的 中声明的 或 状态栏setTheme()的颜色:ActionBarstyles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/brand_red_primary</item>
    <item name="colorPrimaryDark">@color/brand_red_primaryDark</item>
    <item name="android:actionBarStyle">@style/AppTheme.ActionBarStyle</item>
</style>
Run Code Online (Sandbox Code Playgroud)

同样,我的活动中的不同片段有不同的样式colorPrimary和不同的属性。colorPrimaryDark

因此,该setTheme()方法不会更改状态ActionBar栏或状态栏的颜色,但它确实会更改一些内容,例如列表视图中回拉功能的颜色。

我是否遗漏了什么,或者我需要做什么来setTheme()更改ActionBar状态栏颜色?

更新:

我尝试了此处发布的解决方案,但它给出的结果与我之前尝试过的结果完全相同(使用setTheme())。

android android-theme android-fragments navigation-drawer android-styles

2
推荐指数
1
解决办法
3499
查看次数

Android:无法在非静态方法中设置getContext() - 需要API级别23

下面是一些从非静态方法调用的源代码.我收到了错误

"调用需要API级别23(当前最小值为15); android.app.Fragment#getContext"

android.content.Context context = (Context) getContext();
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得上下文对象?

在object explorer> Gradle Scripts> build.gradle下,我看到了这一点.看起来像版本23给我.我在寻找正确的位置吗?

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.test.test"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
}
Run Code Online (Sandbox Code Playgroud)

看起来我有Android Studio 1.5.1.我刚刚在Android Studio中选择了SettingsActivity项目模板.并为该默认代码添加了一个类.

android-studio android-api-levels

2
推荐指数
2
解决办法
4035
查看次数