【问题标题】:accessing a layout(.xml) from a java class in android studio从 android studio 中的 java 类访问布局(.xml)
【发布时间】:2016-10-20 20:02:24
【问题描述】:

我有这个布局(activity_main2.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <include
        layout="@layout/app_bar_main2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The main content view where fragments are loaded -->
    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</android.support.v4.widget.DrawerLayout> 

如您所见,它正在调用另一个布局 (app_bar_main2.xml):

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>
    <include layout="@layout/content_main2" />
</android.support.design.widget.CoordinatorLayout>

这个正在调用另一个布局(content_main2.xml):

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

     <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dear user!"
            android:id="@+id/txtUserName_Main"
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true"/>
</RelativeLayout>

我有一个 java 类(Welcome.Java),它通过以下代码在“onCreate”中调用“activity_main2.xml”:

        setContentView(R.layout.activity_main2);

现在我想从 Welcome.Java 访问 content_main2.xml 中的 TextView ,并将 TextView 的文本设置为我从数据库中读取的真实用户名。 请指导我如何指向 TextView 并设置它的值?

谢谢!

【问题讨论】:

    标签: java xml android-layout android-studio


    【解决方案1】:

    你只需要这样做:

    TextView userName = (TextView) findViewById(R.id.txtUserName_Main);
    userName.setText("YOUR_USERNAME");
    

    【讨论】:

    • 谢谢!我怎么会问这个愚蠢的问题!也许从早上 5 点到现在(晚上)编码太多让我发疯了!
    猜你喜欢
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多