Android Studioで画像とテキストとリストを表示させる画面を作成しています。
xmlファイルで<ImageView>タグ<TextView>タグの下に<TableLayout>タグを配置して、画像とテキストが並列に並んだ下にリストが出るようにします。
javaファイルで配列に格納されたデータが自動的に<TableLayout>タグの中へ表形式で生成されるようコーディングしました。
端末上で実行すると、画像とテキストが表示されず、それらが表示されるべき画面の一番上からリストが出てきてしまいます。
xmlファイルの方で<TableLayout>タグの中に<TableRow>と<TextView>を書き、データをべた打ちで表を作ると
理想通りの配置で表示されます。
またxmlファイルの<TableLayout>とactivity.javaファイルを紐づけるのに
findViewByIdを使うとtableLayout.addViewメソッドでNullPointerExceptionのエラーが発生します。
画像とテキストの下に動的にリストを生成させるには、どうすれば良いでしょうか?
以下、xmlファイルとjavaファイルです。
list.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
</LinearLayout><LinearLayout android:id="@+id/logo_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/image" android:src="@drawable/title_logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" /> <TextView android:id="@+id/date_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginBottom="30dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textSize="20sp" android:layout_alignParentRight="true"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_marginRight="10dp" android:layout_centerHorizontal="true"> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/HorizontalScrollView"> <TableLayout android:id="@+id/member_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true"> </TableLayout> </HorizontalScrollView> </ScrollView> </LinearLayout>
ListActivity.java
package com.example.xxx.practice;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
/**
- Created by XXX on 2016/04/07.
*/
public class ListActivity extends Activity{
private String[][] mPersonal_list = { ~(略)~ }; private final int mFP = ViewGroup.LayoutParams.FILL_PARENT; private final int mWC = ViewGroup.LayoutParams.WRAP_CONTENT; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); int list_length = mPersonal_list.length; TableLayout tableLayout = new TableLayout(this); setContentView(tableLayout); tableLayout = (TableLayout)findViewById(R.id.member_list); //↑↑この一文がないとエラーは発生せず、画像とテキストの位置からリストが表示される。 TextView text_number = new TextView(this); text_number.setText("No."); TextView text_name = new TextView(this); text_name.setText("名前"); TextView text_address = new TextView(this); text_address.setText("メールアドレス"); TableRow tableRow_title = new TableRow(this); tableRow_title.addView(text_number); tableRow_title.addView(text_name); tableRow_title.addView(text_address); tableLayout.addView(tableRow_title, createParam(mFP, mWC)); //↑↑ここでNullPointerException発生。 TextView[] number_count = new TextView[list_length]; ・・・*1 TextView[] name_count = new TextView[list_length]; TextView[] address_count = new TextView[list_length]; TableRow[] row_count = new TableRow[list_length]; for(int i=0; i<list_length; i++){ number_count[i] = new TextView(this); ・・・*2 number_count[i].setText(mPersonal_list[i][0]); name_count[i] = new TextView(this); name_count[i].setText(mPersonal_list[i][1]); address_count[i] = new TextView(this); address_count[i].setText(mPersonal_list[i][2]); row_count[i] = new TableRow(this); row_count[i].addView(number_count[i]); row_count[i].addView(name_count[i]); row_count[i].addView(address_count[i]); tableLayout.addView(row_count[i], createParam(mFP, mWC)); } } private TableLayout.LayoutParams createParam(int w, int h){ return new TableLayout.LayoutParams(w, h); }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/18 07:42
2016/04/18 09:16