将首选项屏幕添加到具有自定义窗口标题的活动
我知道可以向首选项屏幕活动添加按钮。我读了这个问题 如何向 PreferenceScreen 添加按钮 它工作完美。 但如果我想添加自定义标题,我的应用程序就会崩溃。 自定义标题在其自己的活动中完美运行,带有按钮的首选项屏幕在其自己的活动中也完美运行。所以我的问题只是基于将这两个功能结合在一起。
这是我的 java 文件中的代码,
public class TestHeadlineActivity extends PreferenceActivity implements OnClickListener{
/** Called when the activity is first created. */
Button settingsButton;
TextView tv_WindowTitleAccuracy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
addPreferencesFromResource(R.xml.prefs);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
settingsButton = (Button) findViewById(R.id.Button_Settings);
settingsButton.setText("BUTTONTEXT");
settingsButton.setOnClickListener(this);
tv_WindowTitleAccuracy = (TextView) findViewById(R.id.tv_WindowTitleAccuracy);
tv_WindowTitleAccuracy.setText("TEXT");
}
@Override
public void onClick(View v) {
finish();
}
}
这是我的主要 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="10"/>
<Button android:text="This is a button on top of all preferences."
android:layout_height="wrap_content"
android:layout_weight="1" android:layout_width="fill_parent"/>
</LinearLayout>
,这是我的首选项,
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:title="EditText"
android:key="name"
android:summary="Enter your name"
></EditTextPreference>
<CheckBoxPreference
android:title="Music"
android:defaultValue="true"
android:key="checkbox"
android:summary="for the splash screen"
/>
</PreferenceScreen>
谢谢大家
I know its possible to add a button to a prefernces Screeen activtiy. I read this question
How to add a button to PreferenceScreen
and it works perfect.
But if I want to add my custom title my application just crashes.
The customtitle works perfect in its own activity and the preference screen with button works also perfect in its own activity. So my problem is only based on bringing these two functions together.
heres my code in of the java file
public class TestHeadlineActivity extends PreferenceActivity implements OnClickListener{
/** Called when the activity is first created. */
Button settingsButton;
TextView tv_WindowTitleAccuracy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
addPreferencesFromResource(R.xml.prefs);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
settingsButton = (Button) findViewById(R.id.Button_Settings);
settingsButton.setText("BUTTONTEXT");
settingsButton.setOnClickListener(this);
tv_WindowTitleAccuracy = (TextView) findViewById(R.id.tv_WindowTitleAccuracy);
tv_WindowTitleAccuracy.setText("TEXT");
}
@Override
public void onClick(View v) {
finish();
}
}
this is my main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="10"/>
<Button android:text="This is a button on top of all preferences."
android:layout_height="wrap_content"
android:layout_weight="1" android:layout_width="fill_parent"/>
</LinearLayout>
and heres my prefs
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:title="EditText"
android:key="name"
android:summary="Enter your name"
></EditTextPreference>
<CheckBoxPreference
android:title="Music"
android:defaultValue="true"
android:key="checkbox"
android:summary="for the splash screen"
/>
</PreferenceScreen>
thanks guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仍然没有答案?!也许你已经解决了它,但也许其他人会发现其中一些链接有用:
干杯
still no answer?! probably u solved it already but maybe somebody else will find some of this links useful:
cheers