我在首选项屏幕中有一个列表首选项控件,我希望在列表首选项对话框中更改项目的字体大小,我尝试在列表首选项中添加样式,但似乎不起作用,我该怎么办?谢谢!
顺便说一句,我已经阅读了listPreference?中的文章自定义行?,我认为这太复杂了,我只需要更改ListPreference对话框中列出的项目的字体大小。
<ListPreference
style="@style/Text.ListPreference"/>
<style name="Text.ListPreference">
<item name="android:textSize">15sp</item>
</style>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:robobunny="http://robobunny.com"
android:key="AppPreference"
android:summary="@string/PreferenceSummary"
android:title="@string/Preference" >
<PreferenceCategory android:title="@string/PreferenceCallCategory"
android:layout="@layout/preference_category_layout">
<ListPreference
android:key="CallOption"
android:defaultValue="AllNumber"
android:entries="@array/CallAndSMSOption"
android:entryValues="@array/CallAndSMSOption_values"
android:title="@string/CallOptionTitle"
android:summary="@string/CallOptionSummary"
style="@style/myTextLarge"
android:layout="@layout/preference_layout"
/>
</PreferenceCategory>
</PreferenceScreen>
要更改标题和摘要的文本大小,您可以简单地使用自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<TextView android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
编辑:
自定义对话框是一个全新的故事。对话框很难配置。
不幸的是,要更改对话框项的文本大小,您必须为对话框使用自定义适配器。以下是如何从PreessceActive
设置它:
final ListPreference listPreference = (ListPreference) findPreference("CallOption");
if (listPreference == null) {
Log.e("TAG", "Couldn't find the ListPreference");
return;
}
listPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
AlertDialog dialog = (AlertDialog) listPreference.getDialog();
if (dialog == null) {
Log.e(TAG, "Couldn't find the dialog");
return true;
} else {
Log.e(TAG, "Dialog found");
}
ListView listView = dialog.getListView();
if (listView == null) {
Log.e(TAG, "Couldn't find the ListView");
return true;
}
listView.setAdapter(listAdapter);
return true;
}
});
笔记:
创建此custom_pref_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"/>
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"/>
</LinearLayout>
并将其添加到所需的首选项对象中:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="My Category">
<CheckBoxPreference
android:title="I am a Checkbox Preference"
android:defaultValue="false"
android:summary="This is a Checkbox preference"
android:key="checkboxPref"
android:layout="@layout/custom_pref_layout"/>
</PreferenceCategory>
</PreferenceScreen>
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FontPreference extends DialogPreference implements DialogInterface.OnClickListener {
// Keeps the font file paths and names in separate arrays
private List<String> m_fontPaths;
private List<String> m_fontNames;
public FontPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onPrepareDialogBuilder(Builder builder) {
super.onPrepareDialogBuilder(builder);
// Get the fonts on the device
HashMap<String, String> fonts = FontManager.enumerateFonts();
m_fontPaths = new ArrayList<String>();
m_fontNames = new ArrayList<String>();
// Get the current value to find the checked item
String selectedFontPath = getSharedPreferences().getString(getKey(), "");
int idx = 0, checked_item = 0;
for (String path : fonts.keySet()) {
if (path.equals(selectedFontPath))
checked_item = idx;
m_fontPaths.add(path);
m_fontNames.add(fonts.get(path));
idx++;
}
// Create out adapter
// If you're building for API 11 and up, you can pass builder.getContext
// instead of current context
FontAdapter adapter = new FontAdapter();
builder.setSingleChoiceItems(adapter, checked_item, this);
// The typical interaction for list-based dialogs is to have click-on-an-item dismiss the dialog
builder.setPositiveButton(null, null);
}
public void onClick(DialogInterface dialog, int which) {
if (which >= 0 && which < m_fontPaths.size()) {
String selectedFontPath = m_fontPaths.get(which);
Editor editor = getSharedPreferences().edit();
editor.putString(getKey(), selectedFontPath);
editor.commit();
dialog.dismiss();
}
}
// Font adaptor responsible for redrawing the item TextView with the appropriate font.
// We use BaseAdapter since we need both arrays, and the effort is quite small.
public class FontAdapter extends BaseAdapter {
@Override
public int getCount() {
return m_fontNames.size();
}
@Override
public Object getItem(int position) {
return m_fontNames.get(position);
}
@Override
public long getItemId(int position) {
// We use the position as ID
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
// This function may be called in two cases: a new view needs to be created,
// or an existing view needs to be reused
if (view == null) {
// Since we're using the system list for the layout, use the system inflater
final LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// And inflate the view android.R.layout.select_dialog_singlechoice
// Why? See com.android.internal.app.AlertController method createListView()
view = inflater.inflate(android.R.layout.select_dialog_singlechoice, parent, false);
}
if (view != null) {
// Find the text view from our interface
CheckedTextView tv = (CheckedTextView) view.findViewById(android.R.id.text1);
// Replace the string with the current font name using our typeface
Typeface tface = Typeface.createFromFile(m_fontPaths.get(position));
tv.setTypeface(tface);
// If you want to make the selected item having different foreground or background color,
// be aware of themes. In some of them your foreground color may be the background color.
// So we don't mess with anything here and just add the extra stars to have the selected
// font to stand out.
tv.setText(m_fontNames.get(position));
}
return view;
}
}
}
end看到这个回答
在我的应用程序中,我想在我的首选项activity中的单个preferencelist项上设置一个click listener,问题是我只能找到如何在主listprefernce本身上设置一个click listener,有没有一种方法我可以设置一个键到单个列表项,这样当我设置一个OnPreferenceClickListener时,我可以执行一些列表项特定的代码?
问题内容: 我有一个Android应用程序,它使用一些自定义对话框,这些对话框是从XML布局扩展而来的。对话框视图的内容来自XML布局,但是实际的正负按钮是通过调用构建器的setPositiveButton和setNegativeButton方法添加的,因此我无法控制(或至少不知道如何控制)样式按钮本身。 请从我的LoginConfirmationDialog.java文件中查看下面的onCrea
从2015年的谷歌I/O中,我了解到谷歌play服务中有一个新的对话框,用户不需要退出当前应用程序来打开位置。下面的图片显示了它的样子: 现在我的问题是,我如何在我的项目中实现这一点?我已经找了,但没有找到任何有效的答案,请帮助!
当我从我的Android应用程序建立USB连接时,我收到一个Android USB主机权限确认对话框。 我想实现当用户在对话框外点击时,对话框不会被关闭。对此我要做些什么?如何设置用于usb主机权限确认对话框?
我试图通过遵循这里的JetBrain说明,在IntelliJ中运行一个Android项目,但在尝试选择项目模板时遇到一个错误消息(步骤#2)。错误是 “无法创建SDK 您需要首先配置Java SDK“。 我是在mac上做这件事的,并按照上面链接的说明之前的第一页操作。特别是: 在只下载Android sdk内容后,我从终端运行命令。我之所以提到这一点,是因为它说要“运行SDK管理器”,但我在打开的
我正在尝试将使用此查询和值加载到映射中 查询给了我以下值: 1,0|2,1|3,1|4,2|... 然后我尝试将其加载到vaadin网格中,如下所示: 我不能用动态生成的列和一个可编辑的行加载网格,这些列下面有值。我试图使网格看起来像以下: 我试图遵循以下两个链接,但没有成功: > 如何使用地图项目类型填充Vaadin网格 https://vaadin.com/forum/thread/16038