本文介绍了导航标头不显示来自Kotlin中MainActivity的文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个利用Navigation抽屉的应用程序,应该根据用户的输入更改nav_header值,但是我无法这样做,因为这些值永远不会更改.但是,如果我将此值传递给content_main
,则会显示这些值.这是我的布局代码
I have an applcation that utilizes the Navigation drawer and I am supposed to change the nav_header values based on the user's input but I am unable to do that as the values never change. But if i pass this values to the content_main
the values show up. this is my layout code
nav_header_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@color/colorPrimary"
android:gravity="bottom"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
tools:layout_editor_absoluteY="81dp">
<ImageView
android:id="@+id/userImageNavHeader"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="12dp"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:layout_constraintBottom_toTopOf="@+id/loginBtnNavHeader"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/profiledefault" />
<TextView
android:id="@+id/userNameNavHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintStart_toEndOf="@+id/userImageNavHeader"
app:layout_constraintTop_toTopOf="@+id/userImageNavHeader"
tools:text="King Olami" />
<TextView
android:id="@+id/userEmailNavHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toEndOf="@+id/userImageNavHeader"
app:layout_constraintTop_toBottomOf="@+id/userNameNavHeader"
tools:text="[email protected]" />
<Button
android:id="@+id/loginBtnNavHeader"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_marginBottom="8dp"
android:background="@android:color/transparent"
android:onClick="loginBtnNavClicked"
android:text="Login"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageButton
android:id="@+id/addChannelBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/addchannelbutton"
android:onClick="addChannelClicked"
android:padding="5dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
以及我的activity.main中的代码如下所示
and the code for in my activity.main is shown below
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
LocalBroadcastManager.getInstance(this).registerReceiver(userDataChangeReceiver, IntentFilter(BROADCAST_USER_DATA_CHANGED))
}
private val userDataChangeReceiver = object : BroadcastReceiver() {
override fun onReceive(contect: Context?, intent: Intent?) {
if (AuthService.isLoggedIn) {
userNameNavHeader.text = UserDataService.username
mainChannelName.text = UserDataService.email
userEmailNavHeader.text = UserDataService.email
val resourceId = resources.getIdentifier(UserDataService.avatarName, "drawable", packageName)
userImageNavHeader.setImageResource(resourceId)
userImageNavHeader.setBackgroundColor(UserDataService.returnAvatarColor(UserDataService.avatarColor))
loginBtnNavHeader.text = "Logout"
}
}
}
更多代码将根据要求提供.谢谢
further codes would be supplied based on request. Thanks
推荐答案
使用此:
val navigationView = findViewById<NavigationView>(R.id.your_nav_view_id);
val header = navigationView?.getHeaderView(0)
val text = header?.findViewById<TextView>(R.id.textView);
// initialize your widgets in here
// And simply use, "header" prefix before initializing your widgets..
这篇关于导航标头不显示来自Kotlin中MainActivity的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!