[Problem] ImageView.setImageResource verändert Text Gravity der Buttons

  • Antworten:5
  • Bentwortet
Fabian D.
  • Forum-Beiträge: 4

15.07.2012, 18:16:45 via Website

Halöle,

ich hab vor kurzem mit der Android Entwicklung angefangen und stehe gerade vor einem mir unerklärlichen Problem. Und zwar:

Ich habe ein einfaches Relative Layout mit einem ImageView und 4 Buttons darunter. In das ImageView wird beim Programmstart ein Bild geladen, und wenn mein auf einen dieser 4 Buttons klick soll ein anderes Bild angezeigt werden. Problem: Sobald es ein anderes Bild geladen hat änder sich die Text Gravity der Button. Der Text wurde zuerst horizantal wie vertikal als "center" angezeigt, und nach dem Aufruf "setImageResource" ist er vertikal auf einmal "right". Nichtmal der Befehl "button_1.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);" funktioniert...

Hier der Java Quellcode
1public class MainActivity extends Activity implements OnClickListener{
2
3 ImageView imageView1;
4
5 Button button_1;
6 Button button_2;
7 Button button_3;
8 Button button_4;
9
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.activity_main);
14
15 imageView1 = (ImageView)findViewById(R.id.imageView_1);
16
17 button_1 = (Button)findViewById(R.id.button_1);
18 button_2 = (Button)findViewById(R.id.button_2);
19 button_3 = (Button)findViewById(R.id.button_3);
20 button_4 = (Button)findViewById(R.id.button_4);
21
22 button_1.setOnClickListener(this);
23 button_2.setOnClickListener(this);
24 button_3.setOnClickListener(this);
25 button_4.setOnClickListener(this);
26
27 }
28
29 public void onClick(View v) {
30 new ImageChange().execute(v);
31 }
32
33 class ImageChange extends AsyncTask<View, Void, String> {
34
35 @Override
36 protected String doInBackground(View... v) {
37 switch (v[0].getId()){
38 case R.id.button_1:
39 return "SAP";
40 case R.id.button_2:
41 return "SAP";
42 case R.id.button_3:
43 return "Google";
44 case R.id.button_4:
45 return "Google";
46 default:
47 return null;
48 }
49 }
50
51 @Override
52 protected void onPostExecute(String result) {
53 if (result == "SAP")
54 imageView1.setImageResource(R.drawable.sap);
55 if (result == "Google")
56 imageView1.setImageResource(R.drawable.google_logo);
57
58
59 super.onPostExecute(result);
60 }
61 }
62}


Und noch der xml Quellcode vom Layout
1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 xmlns:app="http://schemas.android.com/apk/res/com.example.testapp"
4 android:id="@+id/RelativeLayout_Main"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:gravity="top"
8 android:orientation="vertical" >
9
10 <TableLayout
11 android:id="@+id/TableLayout1"
12 android:layout_width="match_parent"
13 android:layout_height="wrap_content"
14 android:layout_alignParentBottom="true"
15 android:layout_centerHorizontal="true"
16 android:orientation="horizontal" >
17
18 <TableRow
19 android:id="@+id/tableRow1"
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content" >
22
23
24 <Button
25 android:id="@+id/button_1"
26 android:layout_width="wrap_content"
27 android:layout_weight="0.5"
28 android:gravity="center_vertical|center_horizontal"
29 android:text="@string/button_1" />
30
31 <Button
32 android:id="@+id/button_2"
33 android:layout_width="wrap_content"
34 android:layout_weight="0.5"
35 android:text="@string/button_2" />
36 </TableRow>
37
38 <TableRow
39 android:id="@+id/tableRow2"
40 android:layout_width="wrap_content"
41 android:layout_height="wrap_content" >
42
43 <Button
44 android:id="@+id/button_3"
45 android:layout_weight="0.5"
46 android:text="@string/button_3" />
47
48 <Button
49 android:id="@+id/button_4"
50 android:layout_weight="0.5"
51 android:text="@string/button_4" />
52 </TableRow>
53 </TableLayout>
54
55 <RelativeLayout
56 android:layout_width="match_parent"
57 android:layout_height="match_parent"
58 android:layout_above="@+id/TableLayout1"
59 android:layout_alignParentLeft="true"
60 android:layout_alignParentRight="true"
61 android:layout_alignParentTop="true"
62 android:background="#000000" >
63
64 <ImageView
65 android:id="@+id/imageView_1"
66 android:layout_width="match_parent"
67 android:layout_height="match_parent"
68 android:contentDescription="@string/imageView_description"
69 android:padding="10dp"
70 android:src="@drawable/sap" />
71 </RelativeLayout>
72
73</RelativeLayout>

Beim Start


Nach dem Klick links unten auf "Google"


Vielen Dank im Voraus
Fabi

— geändert am 15.07.2012, 18:22:50

Antworten
Michele
  • Forum-Beiträge: 1.525

16.07.2012, 00:26:22 via Website

Vielleicht beim ersten Button android:gravity="center_vertical|center_horizontal rausnehmen?

Sonst wüsste ich nicht was. Müsste ich mal selbs testen.


LG

Antworten
Fabian D.
  • Forum-Beiträge: 4

16.07.2012, 22:07:16 via Website

Hab das Problem gelöst indem ich das Hauptlayout zu einem "linear Layout" gemacht hab ;)

Das ganze sieht jetzt so aus:


Nun hab ich aber das nächste Problem :D Und zwar: Wie bekomm ich die Buttons nach unten und das Logo nach oben? Das Logo wird ja in einem ImageView angezeigt, welcher in einem RelativeLayout ist. Diese RelativeLayout soll den ganzen Rest des Bilschirm füllen (also quasi alles was über den Buttons ist). Die aktuelle xml sieht so aus:

1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 xmlns:app="http://schemas.android.com/apk/res/com.example.testapp"
4 android:id="@+id/LinearLayout1"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent"
7 android:orientation="vertical" >
8
9
10 <TableLayout
11 android:id="@+id/TableLayout1"
12 android:layout_width="match_parent"
13 android:layout_height="100dip"
14 android:layout_gravity="top"
15 android:orientation="horizontal" >
16
17 <TableRow
18 android:id="@+id/tableRow1"
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content" >
21
22 <Button
23 android:id="@+id/button_1"
24 android:layout_column="0"
25 android:layout_weight="0.5"
26 android:gravity="center_vertical|center_horizontal"
27 android:text="@string/button_1" />
28
29 <Button
30 android:id="@+id/button_2"
31 android:layout_column="1"
32 android:layout_weight="0.5"
33 android:gravity="center_vertical|center_horizontal"
34 android:text="@string/button_2" />
35 </TableRow>
36
37 <TableRow
38 android:id="@+id/tableRow2"
39 android:layout_width="wrap_content"
40 android:layout_height="wrap_content" >
41
42 <Button
43 android:id="@+id/button_3"
44 android:layout_weight="0.5"
45 android:gravity="center_vertical|center_horizontal"
46 android:text="@string/button_3" />
47
48 <Button
49 android:id="@+id/button_4"
50 android:layout_weight="0.5"
51 android:gravity="center_vertical|center_horizontal"
52 android:text="@string/button_4" />
53 </TableRow>
54 </TableLayout>
55
56 <RelativeLayout
57 android:id="@+id/relativLayoutMain"
58 android:layout_width="match_parent"
59 android:layout_height="fill_parent"
60 android:layout_below="@id/TableLayout1" >
61
62 <ImageView
63 android:id="@+id/imageView_1"
64 android:layout_width="match_parent"
65 android:layout_height="match_parent"
66 android:contentDescription="@string/imageView_description"
67 android:padding="10dp"
68 android:src="@drawable/sap" />
69 </RelativeLayout>
70
71</LinearLayout>

Antworten
Michele
  • Forum-Beiträge: 1.525

16.07.2012, 22:18:09 via Website

Hattest du meins ausgetestet?

Weil hätte ich schon gerne gewusst bevor du wieder das nächste fragst :)


LG

Antworten
Fabian D.
  • Forum-Beiträge: 4

17.07.2012, 06:37:45 via Website

Ja, hab ich natürlich ausprobiert, hat aber nicht geholfen ;-)

Antworten
Fabian D.
  • Forum-Beiträge: 4

17.07.2012, 21:10:59 via Website

Also, ich hab das Hauptlayout testhalber wieder zu einem RelativeLayout umgewandelt und jetzt funktionierts problemlos... Komische Sache, aber danke nochmal für die Hilfe :wink:

Antworten