Fabian D.
- Forum-Beiträge: 4
15.07.2012, 18:16:45 via Website
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
Und noch der xml Quellcode vom Layout
Beim Start
Nach dem Klick links unten auf "Google"
Vielen Dank im Voraus
Fabi
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}
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>
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
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.