Android更改布局订单

时间:2016-04-04 10:58:10

标签: android

我的listview有这个布局,我希望以编程方式将 04-04 15:27:26.299: W/dalvikvm(9929): threadid=1: thread exiting with uncaught exception (group=0x40f259a8) 04-04 15:27:26.299: E/test(9929): Exception 04-04 15:27:26.310: E/AndroidRuntime(9929): FATAL EXCEPTION: main 04-04 15:27:26.310: E/AndroidRuntime(9929): java.lang.NullPointerException 04-04 15:27:26.310: E/AndroidRuntime(9929): at com.raihanbd.easyrambooster.BatteryCheck.onCreateView(BatteryCheck.java:76) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.view.ViewPager.populate(ViewPager.java:914) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.View.measure(View.java:15698) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.View.measure(View.java:15698) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.View.measure(View.java:15698) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 04-04 15:27:26.310: E/AndroidRuntime(9929): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2197) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.View.measure(View.java:15698) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2171) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1255) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1449) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1145) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4907) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.Choreographer.doCallbacks(Choreographer.java:579) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.Choreographer.doFrame(Choreographer.java:548) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.os.Handler.handleCallback(Handler.java:725) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.os.Handler.dispatchMessage(Handler.java:92) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.os.Looper.loop(Looper.java:153) 04-04 15:27:26.310: E/AndroidRuntime(9929): at android.app.ActivityThread.main(ActivityThread.java:5299) 04-04 15:27:26.310: E/AndroidRuntime(9929): at java.lang.reflect.Method.invokeNative(Native Method) 04-04 15:27:26.310: E/AndroidRuntime(9929): at java.lang.reflect.Method.invoke(Method.java:511) 04-04 15:27:26.310: E/AndroidRuntime(9929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 04-04 15:27:26.310: E/AndroidRuntime(9929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 04-04 15:27:26.310: E/AndroidRuntime(9929): at dalvik.system.NativeStart.main(Native Method) 的顺序更改为TextView之前和之后。这就是它的样子,

LinearLayout

我想将此布局更改为,

<LinearLayout
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/contentWithBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/out_message_bg"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvMessage"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="@android:color/black"
            android:text="TEST TEST"
            android:padding="@dimen/default_layout_padding"
            android:maxWidth="260dp" />

    </LinearLayout>

    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:textSize="12sp"
        android:text="23:21"
        android:textColor="@android:color/darker_gray" />

</LinearLayout>

是否可以单独以编程方式执行此操作?

1 个答案:

答案 0 :(得分:1)

很简单。只需创建两个空的LinearLayouts并将View动态添加到那些。

<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal">

<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"/>

<LinearLayout
    android:id="@+id/contentWithBackground"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/out_message_bg"
    android:orientation="vertical">

    <LinearLayout
    android:id="@+id/layout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="@android:color/black"
        android:text="TEST TEST"
        android:padding="@dimen/default_layout_padding"
        android:maxWidth="260dp" />
    </LinearLayout>
</LinearLayout>

<TextView
    android:id="@+id/tvInfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:textSize="12sp"
    android:text="23:21"
    android:textColor="@android:color/darker_gray" />

</LinearLayout>

现在只需获取TextView,在layout2中调用removeAllViews,在layout1上调用addView(textView)。