1

如何添加/更改 ContextMenu 项的字体?请指导我。

@Override  
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
    super.onCreateContextMenu(menu, v, menuInfo);  
    menu.setHeaderTitle("Select");  
    menu.add(0, v.getId(), 0, "Item1");  
    menu.add(0, v.getId(), 0, "Item2");
    menu.add(0, v.getId(), 0, "Item3");  
}  

我想使用自定义字体 (ttf) 文件更改我的项目的字体。

4

2 回答 2

2

如何添加/更改 ContextMenu 项的字体?

我不认为有对这些东西的内置支持。尽管您可以通过实现 AlertDialog 来实现您的目标。

创建新的Android项目并复制+粘贴相关文件夹中的以下文件并运行应用程序;然后长按TextView。

主要的.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="@+id/hello" android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>

context_menu_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent" android:textStyle="bold">
    <!-- specify android:typeface="" -->
</TextView>

BaseActicity.java

public class BaseActicity extends Activity {

    protected void registerForCustomContextMenu(View targetView, String title, final String[] items) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.context_menu_item, items);
        // You can also specify typeface at runtime by overriding the
        // getView(int position, View convertView, ViewGroup parent) method of ArrayAdapter
        // convertView = super.getView(int position, View convertView, ViewGroup parent)
        // TextView textView = (TextView) convertView;
        // textView.setTypeface(tf, style)
        DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                onCustomContextMenuItemSelected(items, which);
            }
        };
        builder.setAdapter(arrayAdapter, onClickListener);
        OnLongClickListener onLongClickListener = new OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                builder.show();
                return true;
            }
        };
        targetView.setOnLongClickListener(onLongClickListener);
    }

    protected void onCustomContextMenuItemSelected(String[] items, int which) {
    }

}

CustomContextMenuActivity.java

public class CustomContextMenuActivity extends BaseActicity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView hello = (TextView) findViewById(R.id.hello);
        registerForContextMenu(hello);

        final String[] items = {"Red", "Green", "Blue"};
        registerForCustomContextMenu(hello, "Pick a color", items);
    }

    @Override
    protected void onCustomContextMenuItemSelected(String[] items, int which) {
        Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
    }

}

更新

你能告诉我如何在运行时指定 Typoeface 吗?

替换以下代码

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.context_menu_item, items);

通过输入以下代码

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.context_menu_item, items) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = super.getView(position, convertView, parent);
                TextView textView = (TextView) convertView;
                Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/BENGOTHB_0.TTF");
                textView.setTypeface(typeface);
                return convertView;
            }
        }
于 2012-05-22T12:20:56.657 回答
-1

在资产中创建字体文件夹并添加您的 ttf 文件,之后您可以访问您想要的新字体

Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/BENGOTHB_0.TTF");
txt.setTypeface(tf).
于 2012-05-22T11:20:15.063 回答