簡體   English   中英

LayoutInflater不更新TextView

[英]LayoutInflater not updating the TextView

我正在開發一個具有導航抽屜並且需要用戶登錄的應用程序。

到目前為止,我已經完成了整個用戶登錄/注冊的工作,並且已經構建了導航抽屜。

我現在要做的是使導航抽屜上的TextView顯示用戶的全名和他/她的電子郵件。

我已經嘗試使用LayoutInflater來做到這一點,但是TextViews並沒有改變。

我也嘗試過將用戶信息傳遞給LogCat,並正確顯示它。

我不知道我的問題在哪里。

這是我的代碼:

DrawerActivity.java

public class DrawerActivity extends AppCompatActivity{

    public static String[] userInfo = LoginActivity.userInfo;
...
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.navDrawerTheme);
        setContentView(R.layout.activity_drawer_layout);

        //Declare instances.
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
        mNavigationDrawer = (LinearLayout) findViewById(R.id.navigationDrawer);


        LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
        View drawerHeaderView = inflater.inflate(R.layout.drawer_header, null);
        TextView tvFullName = (TextView) drawerHeaderView.findViewById(R.id.name);
        TextView tvEmail = (TextView) drawerHeaderView.findViewById(R.id.email);
        ImageView imgProfile = (ImageView) drawerHeaderView.findViewById(R.id.imgProfile);

        tvFullName.setText(userInfo[0] + " " + userInfo[1]);
        tvEmail.setText(userInfo[2]);
        Log.d("First name", "First name: " + userInfo[0]);
        Log.d("Last name", "Last name: " + userInfo[1]);
        Log.d("Email", "Email: " + userInfo[2]);
...

編輯

cabinet_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#444444">

<RelativeLayout
    android:id="@+id/userInfoWrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_toEndOf="@+id/imgProfile"
    android:layout_alignParentTop="true">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:textSize="14sp"
        android:textStyle="bold"
        android:layout_marginTop="40dp"/>

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="10dp"
        android:textSize="14sp"
        android:textStyle="normal"
        android:layout_below="@id/name"/>

</RelativeLayout>

<de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:src="@drawable/ic_face_white_48dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:id="@+id/imgProfile"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

編輯2

activity_drawer_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Activity Content-->
<FrameLayout
    android:id="@+id/activity_frame"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
</FrameLayout>

<LinearLayout
    android:id="@+id/navigationDrawer"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="start"
    android:layout_width="270dp">

    <include
        android:id="@+id/drawer_header"
        layout="@layout/drawer_header" />

    <ExpandableListView
        android:id="@+id/left_drawer"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:groupIndicator="@android:color/transparent"
        android:divider="@android:color/transparent"
        android:background="#444444"/>

</LinearLayout>

感謝您的時間。

我認為您不需要使用LayoutInflater,只需嘗試一下並讓我知道

TextView tvFullName = (TextView)findViewById(R.id.name);
TextView tvEmail = (TextView)findViewById(R.id.email);
ImageView imgProfile = (ImageView)findViewById(R.id.imgProfile);

tvFullName.setText(userInfo[0] + " " + userInfo[1]);
tvEmail.setText(userInfo[2]);

為澄清起見,它之所以起作用是因為它已經在布局中定義,因此您可以直接訪問它。 如果要使用定義的布局添加一些視圖運行時,請使用LayoutInflater。

您可以嘗試致電

drawerHeaderView.invalidate();

設置文本以刷新視圖后,因為布局已經膨脹。

我可能是錯的,但這可能與獲取LayoutInflater實例的方式有關。 嘗試

LayoutInflater inflater = LayoutInflater.from(getApplicationContext());

像這樣將展開后的視圖附加到父視圖:

RelativeLayout userInfoRL = (RelativeLayout)findViewById(R.id.userInfoWrapper);
userInfoRL.addView(drawerHeaderView);
//just in case use the userInfoRL.invalidate();

試試看,讓我知道它是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM