如何更改图像按钮中的字体?

时间:2014-03-26 06:22:38

标签: android

我是android的新用户。我想改变图像按钮的字体大小。但我的java文件显示错误。任何人都可以告诉我如何在图像按钮中显示文本,还可以更改图像按钮中的字体。我在java文件中的编码如下。

public class AyurvedaMenuActivity extends Activity implements OnClickListener{

ImageButton btnAyurveda, btnDiseases, btnHerbs, btnProducts, btnHospitals, btnResearchCenters, btnDoctors, btnInstitutes, btnContactUs;
TextView tvMainTitle;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ayurveda_menu);


    btnAyurveda = (ImageButton)findViewById(R.id.bAMAyurveda);
    btnContactUs = (ImageButton)findViewById(R.id.bAMContactUs);
    btnDiseases = (ImageButton)findViewById(R.id.bAMDiseases);
    btnDoctors = (ImageButton)findViewById(R.id.bAMDoctors);
    btnHerbs = (ImageButton)findViewById(R.id.bAMHerbs);
    btnHospitals = (ImageButton)findViewById(R.id.bAMHospitals);
    btnInstitutes = (ImageButton)findViewById(R.id.bAMInstitutes);
    btnProducts = (ImageButton)findViewById(R.id.bAMProducts);
    btnResearchCenters = (ImageButton)findViewById(R.id.bAMResearchCenters);
    tvMainTitle=(TextView)findViewById(R.id.tvMainTitle);
    String fontpath =  "fonts/magneto_bold.ttf";
/*String fontpath =  "fonts/forte.ttf";*/
    Typeface tf = Typeface.createFromAsset(getAssets(),fontpath);
    btnAyurveda.setTypeface(tf);
    tvMainTitle.setTypeface(tf);
    btnAyurveda.setTypeface(tf);
    btnDiseases.setTypeface(tf);
    btnContactUs.setTypeface(tf);
    btnDoctors.setTypeface(tf);
    btnHerbs.setTypeface(tf);
    btnHospitals.setTypeface(tf);
    btnInstitutes.setTypeface(tf);
    btnResearchCenters.setTypeface(tf);
    btnContactUs.setTypeface(tf);
    btnProducts.setTypeface(tf);
    btnAyurveda.setOnClickListener(this);
    btnContactUs.setOnClickListener(this);
    btnDiseases.setOnClickListener(this);
    btnDoctors.setOnClickListener(this);
    btnHerbs.setOnClickListener(this);
    btnHospitals.setOnClickListener(this);
    btnInstitutes.setOnClickListener(this);
    btnProducts.setOnClickListener(this);
    btnResearchCenters.setOnClickListener(this);

    //databaseEntry();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.ayurveda_menu, menu);
    return true;
}


@Override
public void onClick(View v) {
    System.out.println("V id"+v.getId());
    switch (v.getId()) {
    case R.id.bAMAyurveda:
        startActivity(new Intent(AyurvedaMenuActivity.this,AyurvedaActivity.class).putExtra("AyurdevaActivity", 1));

        break;

    case R.id.bAMContactUs:
        startActivity(new Intent(AyurvedaMenuActivity.this,ContactUsActivity.class).putExtra("ContactUsActivity", 9));
        break;

    case R.id.bAMDiseases:
        startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 2));
        break;

    case R.id.bAMDoctors:
        startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 7));
        break;

    case R.id.bAMHerbs:
        startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 3));
        break;

    case R.id.bAMHospitals:
        startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 5));
        break;

    case R.id.bAMInstitutes:
        startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 8));
        break;

    case R.id.bAMProducts:
        startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 4));
        break;

    case R.id.bAMResearchCenters:
        startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 6));
        break;

    default:
        break;
    }

}

和我的xml文件如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/b_image"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".AyurvedaMenuActivity" >

<TextView
    android:id="@+id/tvMainTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_image"
    android:gravity="center_horizontal"
    android:text="@string/ayurveda_title"
    android:textColor="#3D0303"
    android:textSize="30sp" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="385dp"
    android:layout_marginTop="5dp"
    android:layout_weight="1.27" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/bAMAyurveda"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_image"
            android:textColor="#3D0303"
            android:text="@string/ayurveda" />

        <ImageButton
            android:id="@+id/bAMDiseases"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:textColor="#3D0303"
            android:background="@drawable/button_image"
            android:text="@string/disease" />

        <ImageButton
            android:id="@+id/bAMHerbs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#3D0303"
            android:background="@drawable/button_image"
            android:text="@string/herbs" />

        <ImageButton
            android:id="@+id/bAMProducts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:textColor="#3D0303"
            android:background="@drawable/button_image"
            android:text="@string/products" />

        <ImageButton
            android:id="@+id/bAMHospitals"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_image"
            android:textColor="#3D0303"
            android:text="@string/hospitals" />

        <ImageButton
            android:id="@+id/bAMResearchCenters"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:textColor="#3D0303"
            android:background="@drawable/button_image"
            android:text="@string/research_centers" />

        <ImageButton
            android:id="@+id/bAMDoctors"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#3D0303"
            android:background="@drawable/button_image"
            android:text="@string/doctors" />

        <ImageButton
            android:id="@+id/bAMInstitutes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:textColor="#3D0303"
            android:background="@drawable/button_image"
            android:text="@string/institutes" />

        <ImageButton
            android:id="@+id/bAMContactUs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#3D0303"
            android:background="@drawable/button_image"
            android:text="@string/contact_us" />
    </LinearLayout>
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/title_image"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/addView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

试试这个..

您无法在android:text

中使用ImageButton

我建议您使用普通按钮,如下所示

<Button 
    android:id="@+id/buttonok" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/button_image"
    android:text="@string/ayurveda"/>

您可以将drawable放在任何地方

android:drawableRight="@drawable/button_image"
android:drawableTop="@drawable/button_image"
android:drawableBottom="@drawable/button_image"

然后您可以添加Typeface,如下所示

 Button btnAyurveda = (Button) findViewById(R.id.bAMAyurveda);
 Typeface tf = Typeface.createFromAsset(getAssets(),fontpath);
 btnAyurveda.setTypeface(tf);

答案 1 :(得分:0)

像这样使用:

Button n=(Button) findViewById(R.id.button1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
n.setText("show");
n.setTypeface(typeface);