2016-06-19 1 views
1

당신이 (app_bar_main2.xml) 다른 레이아웃을 호출 보는 바와 같이 나는이 레이아웃 (activity_main2.xml)안드로이드 스튜디오에서 자바 클래스의 레이아웃 (.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> 

있습니다

<?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> 

내가 자바 클래스 (Welcome.Java)이 C

 setContentView(R.layout.activity_main2); 

지금 나는 Welcome.Java에서 content_main2.xml에서 텍스트 뷰를 액세스하고, 실제 사용자 이름 텍스트 뷰의 텍스트를 설정하려면 다음 코드에 의해 "에서 onCreate"의 ALLS "activity_main2.xml" 나는 데이터베이스에서 읽었습니다. TextView를 가리키고 그 값을 설정하는 방법을 안내해 주시겠습니까?

감사합니다.

답변

1

당신은 할 필요가 :

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

감사합니다! 어떻게이 어리석은 질문을 할 수 있겠습니까! 아마 오전 5시에서 너무 많은 코딩까지 (저녁) 나를 미치게 만들었습니다! – Jasmine

관련 문제