Xutils之View相关问题

最近在学习糗事百科高仿版,而我也好奇的将Xutils加入到了其中,因为在糗事百科中有左侧按钮(SlidingMenu),所以将SlidingMenu布局放在了left_view.xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#413A30"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@mipmap/menu_title_bg">
        <LinearLayout
            android:id="@+id/SettingBtnlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/SettingBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:src="@mipmap/side_setting_icon"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/SettingBtnShuxian"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/SettingBtnlayout"
            android:orientation="horizontal"
            android:background="@mipmap/side_menu_col">
        </LinearLayout>
        <LinearLayout
            android:id="@+id/LoginThisApp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/SettingBtnlayout"
            android:layout_alignTop="@id/SettingBtnlayout"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/SettingBtnShuxian"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingLeft="8dp"
            >
           <ImageView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:background="@mipmap/side_button_background"
               android:src="@mipmap/side_user_avatar"/>
            <TextView
                android:id="@+id/myUserName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|center_horizontal"
                android:padding="5dp"
                android:text="登录糗百"
                android:textColor="#ffffff"
                android:textSize="16sp" />
        </LinearLayout>
    </RelativeLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
           <RelativeLayout
               android:id="@+id/LeftHot"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="@drawable/leftview_list_bg">
               <TextView
                   android:text="热门"
                   android:textColor="#ffffff"
                   android:textSize="16sp"
                   android:paddingLeft="10dp"
                   android:layout_centerVertical="true"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" />

               <ImageView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_alignParentRight="true"
                   android:src="@mipmap/menu_more_details" />

           </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@mipmap/menu_seperator" >
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/LeftNice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/leftview_list_bg">
                <TextView
                    android:text="精华"
                    android:textColor="#ffffff"
                    android:textSize="16sp"
                    android:paddingLeft="10dp"
                    android:layout_centerVertical="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@mipmap/menu_more_details" />

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@mipmap/menu_seperator" >
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/LeftPicture"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/leftview_list_bg">
                <TextView
                    android:text="有图有真相"
                    android:textColor="#ffffff"
                    android:textSize="16sp"
                    android:paddingLeft="10dp"
                    android:layout_centerVertical="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@mipmap/menu_more_details" />

            </RelativeLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@mipmap/menu_seperator" >
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/LeftCross"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/leftview_list_bg" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:text="穿越"
                    android:textColor="#ffffff"
                    android:textSize="16sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@mipmap/menu_more_details" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@mipmap/menu_seperator" >
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/LeftScrip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@drawable/leftview_list_bg" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:text="小纸条"
                    android:textColor="#ffffff"
                    android:textSize="16sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@mipmap/menu_more_details" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@mipmap/menu_seperator" >
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/LeftNear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/leftview_list_bg" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:text="附近"
                    android:textColor="#ffffff"
                    android:textSize="16sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@mipmap/menu_more_details" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@mipmap/menu_seperator" >
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/LeftCheck"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@drawable/leftview_list_bg" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:text="审核新糗事"
                    android:textColor="#ffffff"
                    android:textSize="16sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@mipmap/menu_more_details" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/LeftCheck11111111111"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@drawable/leftview_list_bg" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:text="eoeAndroid3群 苦涩 QQ:534429149\n技术研究,切勿用于商业用途\n一切纠纷与作者无关"
                    android:textColor="#ffffff"
                    android:textSize="16sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:src="@mipmap/menu_more_details" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@mipmap/menu_seperator" >
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

然后问题就出现了

    众所周知,在left_view中的按钮都是通过先Inflate出left_view,然后根据left_view.findViewByid(),并且xUtils只需要在控件上使用注解@ViewInject(id) 然后将需要使用到的控件放在 x.view().inject(this)之后,而我我忽视了布局的记载方式是left_view.findViewByid()草率的使用@ViewInject,然后布局一直无法加载出来研究了一个小时左右,百度了很久还是没找出来原因,然后看了下Xutils的Demo发现出了两种解决方法,还有

void inject(Object handler, View view);
于是产生了两种解决方案:
1.是在Activity类中写一个ViewHolder将你要初始化的控件写在里面,然后在Oncreate()方法中写
x.view().inject(new ViewHolder(),mleftView);
public class ViewHolder{
        // leftView里面的控件

@ViewInject(R.id.xxx)
private Button xxx;

    }


2.就是直接在onCreate方法中使用
x.view().inject(this,mleftView);

显而易见第二中方法更加简单,通过这次的事例让我明白了,学习开源工具更应该去看demo而不是去百度。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值