Zugriff auf TextView außerhalb TabHost

  • Antworten:3
  • Bentwortet
Maria Kugler
  • Forum-Beiträge: 3

19.02.2011, 16:47:07 via Website

Hallo liebe Android-Entwickler,
ich muss derzeit für die Uni eine Android-App programmieren.

Mein Problem hier ist das folgende:
meine Activity ist layouttechnisch in einen oberen und einen unteren Teil aufgeteilt.

Im oberen Teil befindet sich links ein Bild (ImageView) und rechts Name und Status-Nachricht (jeweils TextView).
Im unteren Teil ist ein TabHost mit 3 Tabs.

Anzeigetechnisch funktioniert auch alles.

Nun möchte ich in meiner *.java auf die TextViews im oberen Teil zugreifen um dort den Stil, aber natürlich auch den Inhalt verändern zu können.

Das geht aber irgendwie einfach nicht und die App wir "geforceclosed" sobald ich in der App auf den Button klicke, der zu dieser *.java verweist.

Es wäre nett, wenn mir jemand verraten kann, wie ich in meiner *.java (mit "extends TabActivity") auf die TextViews zugreifen kann, die layouttechnisch eben NICHT im TabHost angesiedelt sind.

Meine Class:

1import android.app.TabActivity;
2import android.content.Intent;
3import android.graphics.Typeface;
4import android.os.Bundle;
5import android.widget.TabHost;
6import android.widget.TextView;
7
8
9public class Profile extends TabActivity
10{
11 private TextView Profile_State;
12
13 @Override
14 public void onCreate(Bundle icicle)
15 {
16 super.onCreate(icicle);
17 setContentView(R.layout.profile);
18
19 Profile_State = (TextView)findViewById(R.id.profile_state);
20
21 Typeface face = Typeface.createFromAsset(getAssets(), "fonts/Sansation.ttf");
22
23 Profile_State.setTypeface(face);
24
25
26 Profile_State.setText("I'm currently programming an Android App :)");
27
28 TabHost tabHost = getTabHost();
29 TabHost.TabSpec spec;
30 Intent intent;
31
32 intent = new Intent().setClass(this, Wall.class);
33 spec = tabHost.newTabSpec("wall").setIndicator("Wall")
34 .setContent(intent);
35 tabHost.addTab(spec);
36
37
38 intent = new Intent().setClass(this, Info.class);
39 spec = tabHost.newTabSpec("info").setIndicator("Info")
40 .setContent(intent);
41 tabHost.addTab(spec);
42
43
44 intent = new Intent().setClass(this, Car.class);
45 spec = tabHost.newTabSpec("car").setIndicator("Car")
46 .setContent(intent);
47 tabHost.addTab(spec);
48
49 tabHost.setCurrentTab(0);
50 }
51
52}


und hier das Layout:
1<?xml version="1.0" encoding="utf-8"?>
2
3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:orientation="vertical"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent"
7 android:background="@drawable/profile_background">
8
9 <LinearLayout android:id="@+id/profile_info"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 android:layout_above="@+id/Profile_Tabs"
13 android:layout_alignParentLeft="true"
14 android:layout_alignParentTop="true"
15 android:orientation="horizontal">
16
17 <!-- Profile Pics im Seitenverhältnis 3:4 -->
18 <ImageButton android:id="@+id/profile_pic"
19 android:layout_width="180dp"
20 android:layout_height="240dp"
21 android:onClick="OnClickPic"
22 android:background="@drawable/profile_pic"
23 android:layout_marginLeft="13dp"
24 android:layout_marginTop="52dp"/>
25
26 <LinearLayout android:id="@+id/profile_state"
27 android:orientation="vertical"
28 android:layout_width="200dp"
29 android:layout_height="wrap_content"
30 android:layout_alignParentRight="true"
31 android:layout_alignParentTop="true">
32
33 <TextView android:id="@+id/profile_prename"
34 android:textSize="30dp"
35 android:textColor="#ffcccccc"
36 android:maxLines="1"
37 android:layout_height="wrap_content"
38 android:layout_marginTop="52dp"
39 android:layout_marginRight="7dp"
40 android:layout_marginLeft="13dp"
41 android:layout_width="180dp"
42 android:text="nachname">
43 </TextView>
44
45 <TextView android:id="@+id/profile_surname"
46 android:textSize="30dp"
47 android:textColor="#ffffffff"
48 android:maxLines="1"
49 android:layout_height="wrap_content"
50 android:layout_marginRight="7dp"
51 android:layout_marginLeft="13dp"
52 android:layout_width="180dp"
53 android:text="vorname">
54
55 </TextView>
56
57 <TextView android:id="@+id/profile_state"
58 android:textSize="20dp"
59 android:textColor="#ffffffff"
60 android:maxLines="3"
61 android:layout_height="120dp"
62 android:layout_marginTop="50dp"
63 android:layout_marginRight="10dp"
64 android:layout_marginLeft="18dp"
65 android:layout_width="172dp" />
66
67
68 </LinearLayout>
69
70
71 </LinearLayout>
72
73 <LinearLayout android:id="@+id/profile_tabhost"
74 android:layout_width="fill_parent"
75 android:layout_height="450dp"
76 android:layout_alignParentRight="true"
77 android:layout_alignParentBottom="true">
78
79 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
80 android:id="@android:id/tabhost"
81 android:layout_width="fill_parent"
82 android:layout_height="wrap_content">
83
84 <LinearLayout
85 android:orientation="vertical"
86 android:layout_width="fill_parent"
87 android:layout_height="fill_parent"
88 android:layout_marginBottom="0dp">
89
90 <TabWidget
91 android:id="@android:id/tabs"
92 android:layout_width="fill_parent"
93 android:layout_height="40dp" />
94 <FrameLayout
95 android:id="@android:id/tabcontent"
96 android:layout_width="fill_parent"
97 android:layout_height="410dp"/>
98 </LinearLayout>
99 </TabHost>
100 </LinearLayout>
101
102
103</LinearLayout>


Vielen Dank auf jeden Fall schonmal im Voraus. Ich hoffe, irgendjemand kann mir da helfen.

Viele Grüße
Maria

Antworten
David Pohlmann
  • Forum-Beiträge: 1

07.04.2011, 10:41:50 via Website

Hallo,

habe exakt das gleiche Problem... Inzwischen eine Lösung gefunden?

lG

Antworten
Maria Kugler
  • Forum-Beiträge: 3

07.04.2011, 10:46:39 via Website

Hi,
den Thread hatte ich ja total vergessen! :D

Ja, habe mittlerweile eine Lösung gefunden.

Da ich gerade (--> Arbeit) keinen Zugriff auf meinen Code habe, werde ich heute Abend meine Lösung posten.

Antworten
Maria Kugler
  • Forum-Beiträge: 3

08.04.2011, 09:40:38 via Website

So,
hier nun meine Lösung:

vorneweg: das wichtige ist die ActivityGroup!

meine Main.java:
1package com.test.blubb.profile;
2
3import android.app.ActivityGroup;
4import android.content.Intent;
5import android.graphics.Color;
6import android.widget.TabHost;
7import android.widget.TextView;
8import android.widget.TabHost.OnTabChangeListener;
9
10import com.test.blubb.R;
11
12
13public class Profile extends ActivityGroup
14{
15 private TextView Prename;
16 private TextView Surname;
17 private TextView State;
18
19 @Override
20 public void onCreate(Bundle icicle)
21 {
22 super.onCreate(icicle);
23 setContentView(R.layout.profile);
24
25
26 Surname = (TextView) findViewById(R.id.profile_surname);
27 Prename = (TextView) findViewById(R.id.profile_prename);
28 State = (TextView) findViewById(R.id.profile_status);
29
30
31 Surname.setText("Nachname");
32 Prename.setText("Vorname");
33 State.setText("I'm currently programming an Android App :)");
34
35
36 tabHost = (TabHost) findViewById(R.id.tabhost_profile);
37 tabHost.setup(getLocalActivityManager());;
38
39 TabHost.TabSpec spec;
40 Intent intent;
41
42 /**
43 * Konstante zur Darstellung der Tab-Beschriftung
44 */
45 final TextView tv1 = new TextView(this);
46 tv1.setText("Tab 1");
47 tv1.setTypeface(face_bold);
48 tv1.setTextColor(Color.RED);
49 tv1.setTextSize(25);
50 tv1.setGravity(17);
51
52 intent = new Intent().setClass(this, Tab1.class);
53 spec = tabHost.newTabSpec("tab1").setIndicator(tv1)
54 .setContent(intent);
55 tabHost.addTab(spec);
56
57 /**
58 * Konstante zur Darstellung der Tab-Beschriftung
59 */
60 final TextView tv2 = new TextView(this);
61 tv2.setText("Tab 2");
62 tv2.setTypeface(face);
63 tv2.setTextColor(Color.WHITE);
64 tv2.setTextSize(25);
65 tv2.setGravity(17);
66
67 intent = new Intent().setClass(this, Tab2.class);
68 spec = tabHost.newTabSpec("tab2").setIndicator(tv2)
69 .setContent(intent);
70 tabHost.addTab(spec);
71
72 /**
73 * Konstante zur Darstellung der Tab-Beschriftung
74 */
75 final TextView tv3 = new TextView(this);
76 tv3.setText("Tab 3");
77 tv3.setTypeface(face);
78 tv3.setTextColor(Color.WHITE);
79 tv3.setTextSize(25);
80 tv3.setGravity(17);
81
82 intent = new Intent().setClass(this, Tab3.class);
83 spec = tabHost.newTabSpec("tab3").setIndicator(tv3)
84 .setContent(intent);
85 tabHost.addTab(spec);
86
87 for(int i=0; i<tabHost.getTabWidget().getChildCount(); i++)
88 {
89 tabHost.getTabWidget().getChildAt(i)
90 .setBackgroundResource(R.drawable.tab_inactive);
91 }
92 tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.WHITE);
93
94
95
96 tabHost.setOnTabChangedListener(new OnTabChangeListener()
97 {
98 public void onTabChanged(String arg0)
99 {
100 for(int i=0; i<tabHost.getTabWidget().getChildCount(); i++)
101 {
102 tabHost.getTabWidget().getChildAt(i)
103 .setBackgroundResource(R.drawable.tab_inactive);
104 }
105 tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.WHITE);
106 if (tabHost.getCurrentTab()==0)
107 {
108 tv1.setTextColor(Color.RED);
109 tv1.setTypeface(face_bold);
110 tv2.setTextColor(Color.WHITE);
111 tv2.setTypeface(face);
112 tv3.setTextColor(Color.WHITE);
113 tv3.setTypeface(face);
114 }
115 else if (tabHost.getCurrentTab()==1)
116 {
117 tv1.setTextColor(Color.WHITE);
118 tv1.setTypeface(face);
119 tv2.setTextColor(Color.RED);
120 tv2.setTypeface(face_bold);
121 tv3.setTextColor(Color.WHITE);
122 tv3.setTypeface(face);
123 }
124 else
125 {
126 tv1.setTextColor(Color.WHITE);
127 tv1.setTypeface(face);
128 tv2.setTextColor(Color.WHITE);
129 tv2.setTypeface(face);
130 tv3.setTextColor(Color.RED);
131 tv3.setTypeface(face_bold);
132 }
133 }
134 });
135 }
136}

und die Layout.xml:
1<?xml version="1.0" encoding="utf-8"?>
2
3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:orientation="vertical"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent"
7 android:background="@drawable/profile_background">
8
9 <LinearLayout android:id="@+id/profile_info"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 android:layout_alignParentLeft="true"
13 android:layout_alignParentTop="true"
14 android:orientation="horizontal">
15
16
17 <TextView android:id="@+id/profile_prename"
18 android:textSize="30dp"
19 android:textColor="#ffcccccc"
20 android:maxLines="1"
21 android:layout_height="wrap_content"
22 android:layout_marginTop="52dp"
23 android:layout_marginRight="7dp"
24 android:layout_marginLeft="13dp"
25 android:layout_width="180dp"/>
26
27 <TextView android:id="@+id/profile_surname"
28 android:textSize="30dp"
29 android:textColor="#ffffffff"
30 android:maxLines="1"
31 android:layout_height="wrap_content"
32 android:layout_marginRight="7dp"
33 android:layout_marginLeft="13dp"
34 android:layout_width="180dp"/>
35
36
37 <TextView android:id="@+id/profile_status"
38 android:textSize="20dp"
39 android:textColor="#ffffffff"
40 android:maxLines="5"
41 android:layout_height="120dp"
42 android:layout_marginTop="9dp"
43 android:layout_marginRight="10dp"
44 android:layout_marginLeft="18dp"
45 android:layout_width="172dp" />
46
47 </LinearLayout>
48
49 <LinearLayout android:id="@+id/profile_tabhost"
50 android:layout_width="fill_parent"
51 android:layout_height="450dp"
52 android:layout_alignParentRight="true"
53 android:layout_alignParentBottom="true">
54
55 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
56 android:id="@+id/tabhost_profile"
57 android:layout_width="fill_parent"
58 android:layout_height="wrap_content">
59
60 <LinearLayout
61 android:orientation="vertical"
62 android:layout_width="fill_parent"
63 android:layout_height="fill_parent"
64 android:layout_marginBottom="0dp">
65
66 <TabWidget
67 android:id="@android:id/tabs"
68 android:layout_width="fill_parent"
69 android:layout_height="40dp" />
70 <FrameLayout
71 android:id="@android:id/tabcontent"
72 android:layout_width="fill_parent"
73 android:layout_height="400dp" >
74 </FrameLayout>
75
76 </LinearLayout>
77 </TabHost>
78 </LinearLayout>
79
80
81</LinearLayout>

Ich hoffe, das hilft dir was! :)

Viel Spaß damit und viele Grüße
Maria

— geändert am 08.04.2011, 09:41:43

Antworten