我创建了一个自定义文本视图类,我正在尝试将其夸大。这是我的代码: -
public class CustomTextView extends TextView{
public CustomTextView(Context context) {
super(context);
LayoutInflater li = LayoutInflater.from(context);
TextView view = (TextView) li.inflate(R.layout.customtextview,null);
view.setBackgroundColor(Color.RED);
}
}
CustomTextview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:text="sgfiughbjkh"
android:id="@+id/customtext"
android:layout_height="match_parent" >
</TextView>
,在我的主要活动xml中,我只有一个线性布局: -
LinearLayout main = (LinearLayout)findViewById(R.id.mainView);
CustomTextView cus = new CustomTextView(this);
main.addView(cus);
我知道我是否将其扩展到线性布局而不是文本视图,并添加一个lienarlayout作为父级,其中textView,它有效。
,但问题在于我想仅用文本视图夸大XML并将其夸大,并且上述代码不起作用。请建议
如何使用布局充气器仅包含一个文本视图的XML?
// inflate text view
TextView textView = (TextView) getLayoutInflater().inflate(R.layout.customtextview, null);
// add it to your view
// in your condition, we will add it to Linear Layout
LinearLayout main = (LinearLayout)findViewById(R.id.mainView);
main.addView(textView);
它已经旧了,两年后,我遇到了同一问题。
您不能从视图延伸并从布局膨胀。该视图需要由ViewGroup包裹。阅读了解Android的Layoutinflater.inflate()
我提出了2个解决方案:
1。在您的类CustomTextView中,您以编程方式设置所有属性。不要夸大任何东西。从资源dimen.xml
2。在您的活动中,夸大文本视图
LinearLayout main = (LinearLayout)findViewById(R.id.mainView);
TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.customtextview, null);
// you might need as well to set the layout params again
// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
// textView.setLayoutParams(params);
main.addView(textView);
有点晚了,但也许会帮助人们。
首先,就像Pratik Goyal所说,您需要声明您的CustomTextView,而不是TextView
<com.example.CustomTextView
android:id="@+id/customTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
然后在您的CustomTextView类中,您需要拥有这些行(构造函数):
public FontFitTextView(final Context context) {
super(context);
}
public FontFitTextView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
和最后(如果我错了的话,请纠正我),但是您不能将自定义视图夸大到文本视图中。
textView textView =(textView)getlayoutinflater()。膨胀(r.layout.customtextview,null);
相反,您需要将r.layout.customtextview放入CustomTextView
CustomTextView customTxtView = (CustomTextView) getLayoutInflater().inflate(R.layout.customlayout, null);
现在,我正在写下来,我认为不可能仅夸大文本视图。如果要夸大r.layout.customlayout,则至少需要一个布局(线性,相对等...),以包装您的CustomTextView
类似这样的东西
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.yourpakagename.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_centerInParent="true"
android:includeFontPadding="false"
android:text="13" />
</RelativeLayout>
您可以在eclipse的图形接口中找到标题" custom&amp; strong>"下的所有自定义创建视图。
如果您的CustomTextView在" com..xample"软件包中,则可以定义自定义组件为:
<com.example.CustomTextView
android:id="@+id/customTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>