0

我的安卓设置有问题。我想创建用于更改活动背景颜色的设置。我需要做什么?

我有布局:

public class MyApp extends PreferenceActivity{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }

public void display(View view) 
{
        Intent intent = new Intent(this, Display.class);

    startActivity(intent);

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{

    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);
    return true;
}


public boolean onOptionsItemSelected( MenuItem item)
{

    switch (item.getItemId())
    {
    case R.id.menu_about:
        Intent menu_about = new Intent(this, About.class);
         startActivity(menu_about);
        return true;

    case R.id.menu_copyright:
        Intent menu_copyright = new Intent(this, Copyright.class);
        startActivity(menu_copyright);

        return true;

    case R.id.menu_settings:

        // ACTIVITY OF SETTINGS
        return true;

    case R.id.menu_exit:

        finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
};

}

我想要这样的偏好:

http://i.stack.imgur.com/k2qA5.png

现在,你明白我的意思了吗?

4

3 回答 3

1

以这种方式创建 SharedPreferences

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
 SharedPreferences.Editor editor = settings.edit();
 editor.putString("PrefName", VALUE);
 editor.commit();

以这种方式获取其值

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
 String s = settings.getString("PrefName", ""));

最后一条语句意味着您正在寻找“PrefName”的值,如果没有找到,则设置“”。

希望能帮助到你

于 2013-05-08T13:39:59.287 回答
1

菜单.xml

 <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item    
        android:id="@+id/settings"    
        android:title="Settings"    
       >
        <menu>
             <item android:id="@+id/red"
                  android:title="Red" />

            <item android:id="@+id/Blue"

                  android:title="Blue" />

    </menu>  
   </item>  

     </menu>

AndroidMenusActivity.java

public class AndroidMenusActivity extends Activity {
   LinearLayout li;
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            li= findViewById(R.id.layoutid);
        }



  @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.layout.menu, menu);
            return true;
        }


  public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
        case R.id.red:
              li.setBackgroundColor("#ff0000");
            break;

        case R.id.blue:
              li.setBackgroundColor("#0000ff");
        break;

        }
    }   

}

主要的.xml

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

</LinearLayout>
于 2013-05-08T14:11:32.647 回答
0

您的意思是通过 XML 文件更改活动背景吗?

android:background="@android:color/xxxxx"

选择列出的颜色之一。

于 2013-05-08T13:46:09.653 回答