将TextView从xml加载到TextSwitcher中

时间:2012-07-22 01:15:15

标签: java android xml textview

我一直在尝试从xml文件中加载TextView来设置TextSwitcher。我有一个基本的TextSwitcher示例,它使用一个空的TextView工作正常:

.java

public class TextSwitcherTest extends Activity 
{
    private TextSwitcher textSwitcher;

    private ViewFactory viewFactory = new ViewFactory() 
    {
        public View makeView() 
        {
            TextView textView = new TextView(TextSwitcherTest.this);

            return textView;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);

        textSwitcher.setFactory(viewFactory);

        Animation in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this,android.R.anim.fade_out);

        textSwitcher.setInAnimation(in);
        textSwitcher.setOutAnimation(out);

        textSwitcher.setText("test ok");
    }    
}

-

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextSwitcher 
        android:id="@+id/textSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

但是当我更改代码以尝试从xml文件加载TextView时,它会吐出一堆错误。这是修改后的代码:

.java

public class TextSwitcherTest extends Activity 
{
    private TextSwitcher textSwitcher;

    private ViewFactory viewFactory = new ViewFactory() 
    {
        public View makeView() 
        {
            LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this);

            TextView textView = (TextView) inflater.inflate(R.id.textView,null);

            return textView;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);

        textSwitcher.setFactory(viewFactory);

        Animation in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this,android.R.anim.fade_out);

        textSwitcher.setInAnimation(in);
        textSwitcher.setOutAnimation(out);

        textSwitcher.setText("test ok");
    }    
}

-

textview.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

任何人都可以提出我出错的建议吗?代码编译好了,但我确定它是以某种方式滥用LayoutInflater?


错误消息(它没有显示'11更多'):

07-22 03:51:24.096: W/dalvikvm(580): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-22 03:51:24.137: E/AndroidRuntime(580): FATAL EXCEPTION: main
07-22 03:51:24.137: E/AndroidRuntime(580): java.lang.RuntimeException: Unable to start activity ComponentInfo{test09.TextSwitcher01/test09.TextSwitcher01.TextSwitcherTest}: android.content.res.Resources$NotFoundException: Resource ID #0x7f050001 type #0x12 is not valid
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.os.Looper.loop(Looper.java:137)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.app.ActivityThread.main(ActivityThread.java:4424)
07-22 03:51:24.137: E/AndroidRuntime(580):  at java.lang.reflect.Method.invokeNative(Native Method)
07-22 03:51:24.137: E/AndroidRuntime(580):  at java.lang.reflect.Method.invoke(Method.java:511)
07-22 03:51:24.137: E/AndroidRuntime(580):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-22 03:51:24.137: E/AndroidRuntime(580):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-22 03:51:24.137: E/AndroidRuntime(580):  at dalvik.system.NativeStart.main(Native Method)
07-22 03:51:24.137: E/AndroidRuntime(580): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f050001 type #0x12 is not valid
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2110)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.content.res.Resources.getLayout(Resources.java:857)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-22 03:51:24.137: E/AndroidRuntime(580):  at test09.TextSwitcher01.TextSwitcherTest$1.makeView(TextSwitcherTest.java:23)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.widget.ViewSwitcher.obtainView(ViewSwitcher.java:80)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.widget.ViewSwitcher.setFactory(ViewSwitcher.java:99)
07-22 03:51:24.137: E/AndroidRuntime(580):  at test09.TextSwitcher01.TextSwitcherTest.onCreate(TextSwitcherTest.java:38)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.app.Activity.performCreate(Activity.java:4465)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-22 03:51:24.137: E/AndroidRuntime(580):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-22 03:51:24.137: E/AndroidRuntime(580):  ... 11 more

4 个答案:

答案 0 :(得分:16)

您的代码有几个问题:

首先,LayoutInflater.inflate()方法需要R.layout.layout_name形式的布局文件的ID,而不是R.id.some_id的形式:

TextView textView = (TextView) inflater.inflate(R.layout.textView, null);

其次,您使用的代码会抛出ClassCastException,因为当您尝试投射时,膨胀布局的根是LinearLayout而不是TextView。你的代码应该是:

LinearLayout ll =(Linearlayout)inflater.inflate(R.layout.textView,null);

但是,即使上面这一行也行不通,因为正如其名称所暗示的那样TextSwitcher只会采用TextView类型的儿童,TextSwitcherTextView之间不会有任何内容<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> {1}}孩子们。最终要使用的代码是正确的:

textview.xml 的布局:

viewFactory

private ViewFactory viewFactory = new ViewFactory() { public View makeView() { LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this); TextView textView = (TextView) inflater.inflate(R.layout.textView, null); return textView; } }; 字段将是:

{{1}}

答案 1 :(得分:2)

使用大多数XML非常简单。

在XML中添加TextSwitcher根视图和其中的两个TextView:

<TextSwitcher
    android:id="@+id/textswitcher_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:inAnimation="@android:anim/fade_in"
    android:outAnimation="@android:anim/fade_out">

    <TextView
        android:id="@+id/tv_onboarding_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text1" />

    <TextView
        android:id="@+id/tv_onboarding_letsgo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text2" />

</TextSwitcher>

然后在代码中,您希望动画操作发生在何处:

使用Kotlin:

textswitcher_id.setText(getString("Text2"))

// and somewhere else, switch it back:

textswitcher_id.setText(getString("Text1"))

或者在Java中:

findViewById(R.id.textswitcher_id).setText(getString("Text2"));

// and somewhere else, switch it back:

findViewById(R.id.textswitcher_id).setText(getString("Text1"));

注意:

  • 在XML中,您还可以使用两个包含而非TextView两次,请参阅:https://stackoverflow.com/a/42724239/1852191

  • 我们在XML中使用默认文本的原因即使我们稍后以编程方式设置它也是因为在第一个视图中它将为空白,直到它设置为

答案 2 :(得分:1)

实际上,如果在布局XML中TextViews放置两个TextSwitcher,则不需要设置工厂。这对我有用:

<TextSwitcher
    android:id="@+id/id1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inAnimation="@android:anim/fade_in"
    android:outAnimation="@android:anim/fade_out">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="1"/>
</TextSwitcher>

(代码中没有进行其他配置,只有inflatefindViewById

但是,您必须在TextViews中复制属性。将TextView移动到单独的文件然后再包含两次可以避免这种情况:

<TextSwitcher
    android:id="@+id/id1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inAnimation="@android:anim/fade_in"
    android:outAnimation="@android:anim/fade_out">

    <include layout="@layout/titlebar" />
    <include layout="@layout/titlebar" />
</TextSwitcher>

避免重复的其他方法是创建自定义样式并将其应用于TextViews

答案 3 :(得分:0)

我提出了这个解决方案,对我而言,最接近于直接在XML中定义TextView:

首先,使用自定义定义

定义TextSwitcher,其中包含两个TextView对象
        <TextSwitcher
            android:fontFamily="sans-serif-condensed"
            android:id="@+id/detail_date_textswitch"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <TextView
                android:id="@+id/detail_date_textview1"
                android:textColor="@color/grey_700"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"/>
            <TextView
                android:id="@+id/detail_date_textview2"
                android:textColor="@color/grey_700"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"/>
        </TextSwitcher>

之后,在您的代码中,为TextSwitcher对象定义以下工厂:

    textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
        boolean isFirst = true;
        @Override
        public View makeView() {
            if (isFirst) {
                isFirst = false;
                TextView textView = (TextView) mDateView.findViewById(R.id.detail_date_textview1);
                mDateView.removeViewAt(0);
                return textView;
            } else {
                TextView textView = (TextView) mDateView.findViewById(R.id.detail_date_textview2);
                mDateView.removeViewAt(0);
                return textView;
            }
        }
    });

我不会说它不是那么棘手,但它对我有用。