我想创建一个表格,其中的列文字取决于微调器。稍后添加行,我可以为指定的单元格着色。它看起来像这样:http://tinypic.com/view.php?pic=2j639xf&s=8#.U-mRRpD-Lcs 这个例子是风景,因为我在dekstop上开发它,所以我想要一个像android那样的表格。
问题是: 1.当我在设备上运行它时,旋转器就消失了 我还不知道如何为表格中的每个单元格着色。或者我应该提出一个新问题?
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reservation_schedule);
Spinner spinnerFacility = (Spinner)findViewById(R.id.spinnerFacility);
ArrayList<String> alTop = getTimeTableColumnTop((int)spinnerFacility.getSelectedItemId());
String[] column = alTop.toArray(new String[alTop.size()]);
ArrayList<String> alLeft = getTimeTableColumnLeft();
String[] row = alLeft.toArray(new String[alLeft.size()]);
int rl=row.length+1; int cl=column.length+1;
Log.d("--", "R-Lenght--"+rl+" "+"C-Lenght--"+cl);
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
private ArrayList<String> getTimeTableColumnTop(int facilitySelectedIndex)
{
ArrayList<String> timeTableColumnTop = new ArrayList<String>();
if(facilitySelectedIndex == 0)
{
timeTableColumnTop.add("Discuss Room 1");
timeTableColumnTop.add("Discuss Room 2");
timeTableColumnTop.add("Discuss Room 3");
}else
{
timeTableColumnTop.add("Pri-View 1");
timeTableColumnTop.add("Pri-View 2");
timeTableColumnTop.add("Pri-View 3");
timeTableColumnTop.add("Pri-View 4");
timeTableColumnTop.add("Pri-View 5");
}
return timeTableColumnTop;
}
private ArrayList<String> getTimeTableColumnLeft()
{
ArrayList<String> timeTableColumnLeft = new ArrayList<String>();
for(int i = 8 ; i<17; i++)
{
timeTableColumnLeft.add(i+":00");
}
return timeTableColumnLeft;
}
对于函数createTableLayout(row,column,rl,cl);我使用http://www.prandroid.com/2014/05/creating-table-layout-dynamically-in.html
中的代码activity_reservation_schedule.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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/reservation_spinner_facility_prompt"/>
<Spinner
android:id="@+id/spinnerFacility"
android:prompt="@string/reservation_spinner_facility_prompt"
android:entries="@array/reservation_facility_entries"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
这是strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ReservationTab(Schedule)</string>
<string name="reservation_spinner_facility_prompt">Pick a facility</string>
<string-array name="reservation_facility_entries">
<item>Discussion Room</item>
<item>Private Viewing</item>
</string-array>
</resources>
注意:我是Android上的新手,我使用的是eclipse。这是我的第一篇帖子,请原谅我的英语。任何建议都表示赞赏,谢谢。
答案 0 :(得分:0)
几点:
如果我理解正确的话,
e.g
示例代码:
@覆盖 protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.activity_main);
ll = (LinearLayout)findViewById(R.id.ll);
Spinner spinnerFacility = (Spinner)findViewById(R.id.spinnerFacility);
spinnerFacility.setOnItemSelectedListener(new CustomOnItemSelectedListener());
// more of your code.....
ll.addView(sv) // instead of setContentView(sv)
public class CustomOnItemSelectedListener实现OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
ll.removeView(sv);
ArrayList<String> alTop = getTimeTableColumnTop(pos);
String[] column = alTop.toArray(new String[alTop.size()]);
ArrayList<String> alLeft = getTimeTableColumnLeft();
String[] row = alLeft.toArray(new String[alLeft.size()]);
int rl=row.length+1; int cl=column.length+1;
Log.d("--", "R-Lenght--"+rl+" "+"C-Lenght--"+cl);
sv = new ScrollView(getApplicationContext());
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(getApplicationContext());
hsv.addView(tableLayout);
sv.addView(hsv);
ll.addView(sv);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}