我要用波斯语编写一个justified textview
,这个Android TextView Justify Text链接解决方案中的大多数并没有真正帮助,除了其中一个解决方案就是该链接https://github.com/navabi/JustifiedTextView。
我实现了这样的代码,但是无法启动活动ComponentInfo,并且说Binary XML file line #21: Error inflating class ir.noghteh.JustifiedTextView need to be inflated
,但是我该如何膨胀呢。
这是我的代码:
主要活动:
package com.example.android.justify;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private JustifiedTextView mJTv;
private TextView mTv;
private SeekBar mSbSize;
private LinearLayout mLlGreen, mLlRed;
private TextView mTvEng, mTvFa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
mJTv = (JustifiedTextView) findViewById(R.id.activity_main_jtv);
mTv = (TextView) findViewById(R.id.activity_main_tv);
mLlGreen = (LinearLayout) findViewById(R.id.activity_main_ll_green);
mLlRed = (LinearLayout) findViewById(R.id.activity_main_ll_red);
mTvEng = (TextView) findViewById(R.id.activity_main_tv_eng);
mTvFa = (TextView) findViewById(R.id.activity_main_tv_fa);
mSbSize = (SeekBar) findViewById(R.id.activity_main_sb_size);
mSbSize.setMax(20);
mSbSize.setProgress(10);
/* mSbSize.setOnSeekBarChangeListener(this);
mLlGreen.setOnClickListener(this);
mLlRed.setOnClickListener(this);
mTvEng.setOnClickListener(this);
mTvFa.setOnClickListener(this);*/
}
public void onProgressChanged(SeekBar arg0, int pos, boolean arg2) {
int size = 10;
mTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, pos + size);
mJTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, pos + size);
mJTv.setLineSpacing(pos - size);
mTv.setLineSpacing(pos - size, 1);
}
}
我添加了JustifiedTextView作为一个类,还添加了XmlToClassAttribHandler
他还说要添加的课程在这里:
package com.example.android.justify;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
public class XmlToClassAttribHandler {
private Resources mRes;
private Context mContext;
private AttributeSet mAttributeSet;
private String namespace="http://noghteh.ir";
private final String KEY_TEXT="text";
private final String KEY_TEXT_SIZE="textSize";
private final String KEY_TEXT_COLOR="textColor";
public XmlToClassAttribHandler(Context context,AttributeSet attributeSet){
mContext=context;
mRes=mContext.getResources();
mAttributeSet=attributeSet;
}
public String getTextValue(){
String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT);
if (value==null)
return "";
if (value.length()>1 &&
value.charAt(0)=='@' &&
value.contains("@string/")){
int resId=mRes.getIdentifier(mContext.getPackageName()+":"+value.substring(1), null,null);
value=mRes.getString(resId);
}
return value;
}
public int getColorValue(){
String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT_COLOR);
int color= Color.BLACK;
if (value==null)
return color;
if (value.length()>1 &&
value.charAt(0)=='@' &&
value.contains("@color/")){
int resId=mRes.getIdentifier(mContext.getPackageName()+":"+value.substring(1), null,null);
color=mRes.getColor(resId);
return color;
}
try{
color=Color.parseColor(value);
}
catch(Exception e){
return Color.BLACK;
}
return color;
}
public int getTextSize() {
int textSize=12;
String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT_SIZE );
if (value==null)
return textSize;
if (value.length()>1 &&
value.charAt(0)=='@' &&
value.contains("@dimen/")){
int resId=mRes.getIdentifier(mContext.getPackageName()+":"+value.substring(1), null,null);
textSize=mRes.getDimensionPixelSize(resId);
return textSize;
}
try{
textSize=Integer.parseInt(value.substring(0, value.length()-2));
}
catch(Exception e){
return 12;
}
return textSize;
}
public int gettextSizeUnit() {
String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT_SIZE );
if (value==null)
return TypedValue.COMPLEX_UNIT_SP;
try{
String type=value.substring(value.length()-2, value.length());
if (type.equals("dp"))
return TypedValue.COMPLEX_UNIT_DIP;
else if (type.equals("sp"))
return TypedValue.COMPLEX_UNIT_SP;
else if (type.equals("pt"))
return TypedValue.COMPLEX_UNIT_PT;
else if (type.equals("mm"))
return TypedValue.COMPLEX_UNIT_MM;
else if (type.equals("in"))
return TypedValue.COMPLEX_UNIT_IN;
else if (type.equals("px"))
return TypedValue.COMPLEX_UNIT_PX;
}
catch(Exception e){
return -1;
}
return -1;
}
}
这是我得到的错误:
06-19 19:52:01.757 11580-11580/com.example.android.justify E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.justify/com.example.android.justify.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class ir.noghteh.JustifiedTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class ir.noghteh.JustifiedTextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.android.justify.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: ir.noghteh.JustifiedTextView
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.view.LayoutInflater.createView(LayoutInflater.java:552)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.android.justify.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
似乎该错误表明我需要给
ir.noghteh.JustifiedTextView
充气,但是我该怎么做和主要活动代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:text="JustifiedTextView:"
android:textColor="@color/blue" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gray_2" >
<ir.noghteh.JustifiedTextView
xmlns:noghteh="http://noghteh.ir"
android:id="@+id/activity_main_jtv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="25dp"
noghteh:text="@string/text_fa"
noghteh:textColor="@color/red"
noghteh:textSize="20sp" >
</ir.noghteh.JustifiedTextView>
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:text="Default TextView:"
android:textColor="@color/blue" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gray_2" >
<TextView
android:id="@+id/activity_main_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="25dp"
android:text="@string/text_fa"
android:textColor="@color/red"
android:textSize="20sp" />
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:orientation="horizontal"
android:padding="5dp" >
<SeekBar
android:id="@+id/activity_main_sb_size"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</SeekBar>
<LinearLayout
android:id="@+id/activity_main_ll_green"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/green"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is text"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/activity_main_ll_red"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/red"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is text"
/>
</LinearLayout>
<TextView
android:id="@+id/activity_main_tv_fa"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/gray_3"
android:gravity="center"
android:padding="5dp"
android:text="FA" />
<TextView
android:id="@+id/activity_main_tv_eng"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/gray_3"
android:gravity="center"
android:padding="5dp"
android:text="EN" />
</LinearLayout>
</LinearLayout>
最佳答案
如下更改您的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:text="JustifiedTextView:"
android:textColor="@color/blue" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gray_2" >
<com.example.android.justify.JustifiedTextView
xmlns:noghteh="http://noghteh.ir"
android:id="@+id/activity_main_jtv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="25dp"
noghteh:text="@string/text_fa"
noghteh:textColor="@color/red"
noghteh:textSize="20sp" >
</com.example.android.justify.JustifiedTextView>
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:text="Default TextView:"
android:textColor="@color/blue" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gray_2" >
<TextView
android:id="@+id/activity_main_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="25dp"
android:text="@string/text_fa"
android:textColor="@color/red"
android:textSize="20sp" />
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:orientation="horizontal"
android:padding="5dp" >
<SeekBar
android:id="@+id/activity_main_sb_size"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</SeekBar>
<LinearLayout
android:id="@+id/activity_main_ll_green"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/green"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is text"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/activity_main_ll_red"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/red"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is text"
/>
</LinearLayout>
<TextView
android:id="@+id/activity_main_tv_fa"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/gray_3"
android:gravity="center"
android:padding="5dp"
android:text="FA" />
<TextView
android:id="@+id/activity_main_tv_eng"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/gray_3"
android:gravity="center"
android:padding="5dp"
android:text="EN" />
</LinearLayout>
</LinearLayout>