我正在尝试在一个按钮内使用两个带有不同字体的TextView自定义按钮.为此,我只是扩展了Button并在构造函数中编写了以下代码,
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.custom_button,
(ViewGroup) findViewById(R.id.custom_button_view));
TextView firstTextView = (TextView) layout
.findViewById(R.id.firstTextView);
TextView secondTextView = (TextView) layout
.findViewById(R.id.secondTextView);
Run Code Online (Sandbox Code Playgroud)
在布局custom_button我已经放置了两个具有不同字体和文本的TextView,custom_button_view是该LinearLayout的ID,我得到的是一个没有文本的空按钮.
任何想法,谢谢.
小智 8
您可以通过将自定义按钮样式设置为布局来使用布局作为按钮,并可以向其添加两个textView,方式如下:
<LinearLayout android:id="@+id/customButtonLayout"
android:layout_height="wrap_content" style="@android:style/Widget.Button"
android:layout_width="wrap_content">
<TextView android:text="First" android:id="@+id/firstTextView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000"></TextView>
<TextView android:textColor="#000" android:text="Second"
android:layout_height="wrap_content" android:id="@+id/secondTextView"
android:layout_width="wrap_content" android:layout_marginLeft="10dp"></TextView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在Activity中你可以设置不同的字体:
Typeface font=Typeface.createFromAsset(getAssets(),"ARIALN.TTF") ;
Typeface font2=Typeface.createFromAsset(getAssets(), "COMPCTAN.TTF");
TextView firstTextView = (TextView)findViewById(R.id.firstTextView);
TextView secondTextView = (TextView)findViewById(R.id.secondTextView);
firstTextView.setTypeface(font);
secondTextView.setTypeface(font2);
LinearLayout btnLayout=(LinearLayout) findViewById(R.id.customButtonLayout);
btnLayout.setOnClickListener(this);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7859 次 |
| 最近记录: |