2017-01-21 41 views
0

將視圖的XML代碼直接重新創建到Java代碼後,它停止正常工作。移動到代碼後的Android xml不起作用

的XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.virviil.splashscreen.MainActivity"> 


    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.7" /> 
    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center_horizontal"> 

     <ProgressBar 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </TableRow> 
    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.3" /> 
</TableLayout> 

和Java代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    TableLayout activity_main = new TableLayout(this); 
    activity_main.setId(R.id.activity_main); 
    LayoutParams layout_439 = new LayoutParams(0,0); 
    layout_439.width = LayoutParams.MATCH_PARENT; 
    layout_439.height = LayoutParams.MATCH_PARENT; 
    activity_main.setLayoutParams(layout_439); 

    TableRow tableRow_879 = new TableRow(this); 
    LayoutParams layout_311 = new LayoutParams(0,0); 
    layout_311.width = LayoutParams.MATCH_PARENT; 
    layout_311.height = LayoutParams.MATCH_PARENT; 
    layout_311.weight = 0.7f; 
    tableRow_879.setLayoutParams(layout_311); 
    activity_main.addView(tableRow_879); 

    TableRow tableRow_600 = new TableRow(this); 
    tableRow_600.setGravity(Gravity.CENTER_HORIZONTAL); 
    LayoutParams layout_61 = new LayoutParams(0,0); 
    layout_61.width = LayoutParams.MATCH_PARENT; 
    layout_61.height = LayoutParams.MATCH_PARENT; 
    tableRow_600.setLayoutParams(layout_61); 

    ProgressBar progressBar_343 = new ProgressBar(this); 
    LayoutParams layout_81 = new LayoutParams(0,0); 
    layout_81.width = LayoutParams.WRAP_CONTENT; 
    layout_81.height = LayoutParams.WRAP_CONTENT; 
    progressBar_343.setLayoutParams(layout_81); 
    tableRow_600.addView(progressBar_343); 
    activity_main.addView(tableRow_600); 

    TableRow tableRow_315 = new TableRow(this); 
    LayoutParams layout_574 = new LayoutParams(0,0); 
    layout_574.width = LayoutParams.MATCH_PARENT; 
    layout_574.height = LayoutParams.MATCH_PARENT; 
    layout_574.weight = 0.3f; 
    tableRow_315.setLayoutParams(layout_574); 
    activity_main.addView(tableRow_315); 

    setContentView(activity_main); 
    //setContentView(R.layout.activity_main); 
} 

的想法是可編程改變的進展vertival aligment。 因此,在XML中它的工作enter image description here 而在JAVA中 - 不是。

+0

把它放在Linearlayout中 –

+0

但xml的作品。所以即使我將一切都改爲linearLayout,爲什麼java不起作用? – Virviil

+0

TableLayout activity_main =(TableLayout)findViewById(R.id.activity); –

回答

0

你唯一需要的是更多的關注和使用的LinearLayout代替OD TableLayout整個XML視圖。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.virviil.splashscreen.MainActivity"> 
<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

.... 
+0

那什麼都不做。我將所有內容添加到linearLayout中 - 但仍然無法正常工作 – Virviil