2011-12-23 6 views
0

Android用のコードを書く際に問題が発生しています。私はEclipse開発環境を使用しています。私が達成しようとしているのは、週の曜日をテーブルの最初の行(6行)に均等に配置することです。言葉が実際に広がり、集団に集まるだけではないようにする方法を理解できません。 (はい、私は体重を使って試しました)。私のコードは以下の通りです。コード内の質問やコメントのスペルミスがあれば、事前にお詫び申し上げます。Android LinearLayout Spacing

package A3.cal; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.TableLayout; 
import android.widget.TableRow; 

public class A3Activity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); //it works dont touch it... 
    setContentView(R.layout.main); //old Content View 
    MonthView(); //setup the initial view 
} 

public void MonthView(){ 
    TableRow[] tableRows = new TableRow[6]; //create table rows for the table 
    LinearLayout[] dayHeaderFrames = new LinearLayout[7]; //create the frame layouts for the table headers 
    TextView[] days = new TextView[7]; //create new text views array 
    days = populateDays(0); //make the textviews say the proper days (this can be faster by manipulating the strings and then adding to textview) 
    TableLayout monthView = (TableLayout) findViewById(R.id.monthView); //find month View in the xml file 
    LinearLayout headerLayout = new LinearLayout(this); //create the linear layout that will hold the frame layouts for the text views** 
    headerLayout.setBackgroundColor(Color.WHITE); //make the linear layouts white 
    LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f); //create the layout perameters for childeren 

    for(int i = 0; i < 6; i++){ //create the table rows 
     tableRows[i] = new TableRow(this); //create the new rows 
    } 

    tableRows[0].addView(headerLayout); //add the header layout to the first row 
    tableRows[0].setBackgroundColor(Color.RED); //set the background table row (only the first one which I am concentrating on) to red 
    monthView.setBackgroundColor(Color.BLUE); // make the table blue 

    for(int i = 0; i < 7; i++){ //add the headers to the month day header row 
     dayHeaderFrames[i] = new LinearLayout(this); //create new frames 
     headerLayout.addView(dayHeaderFrames[i], ll); //add the frames to the first row in the table 
     dayHeaderFrames[i].addView(days[i]); //add the textview objects to the frames (the text view objects already have the correct text from when populate days was called) 
    } 

    for(int i = 0; i < 6; i++){ //add the rows to the month view 
     monthView.addView(tableRows[i]); //add the new rows to the table 
    } 
} 

public TextView[] populateDays(int startDay){ 
    TextView[] daysTextViewArr = new TextView[7]; //create a textview array 
    String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //days of the week 

    for(int i = 0; i < 7; i++){ //put the days into the order based upon the start day 
     daysTextViewArr[i] = new TextView(this); //create a new text view object 

     //the following logic puts the days into order 
     int day = i + startDay; 
     if(day > 6){ //if the day will exceed the array put it back to the begining 
      day = day - 7; 
     } 

     daysTextViewArr[i].setText(days[i]); //set the text in the text view to the propper day 
    } 

    return daysTextViewArr; 
} 

} 

そしてメイン

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ll" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal"> 


<TableLayout 
    android:id="@+id/monthView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</TableLayout> 

コードが線形レイアウトを行う間隔を除いて働くおさらいにXMLレイアウト/ textviews

私は事前にあなたのすべてに感謝したいですこの(そして他の多くの)問題について私を助けてくれた。

敬具、

リード

答えて

1

ソリューションは簡単です。あなたはandroid:stretchColumns属性を使う必要があります。 あなたのコード

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ll" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 


<TableLayout 
android:id="@+id/monthView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:stretchColumns="0,1,2,3,4,5"> 

</TableLayout> 
</LinearLayout> 

Javaコード

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    getView(); 
} 


    public TextView[] populateDays(int startDay) { 
    TextView[] daysTextViewArr = new TextView[7]; 
    String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday", 
      "Thursday", "Friday", "Saturday" }; 
    for (int i = 0; i < 7; i++) { 
     daysTextViewArr[i] = new TextView(this); 
     int day = i + startDay; 
     if (day > 6) { 
      day = day - 7; 
     } 
     daysTextViewArr[i].setText(days[i]); 
    } 

    return daysTextViewArr; 
} 

public void getView() { 
    TableRow[] tableRows = new TableRow[6]; 

    TextView[] days = new TextView[7]; 
    days = populateDays(0); 
    TableLayout monthView = (TableLayout) findViewById(R.id.monthView); 
    LinearLayout headerLayout = new LinearLayout(this); 
    headerLayout.setBackgroundColor(Color.WHITE); 
    LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT, 1f); // create the 

    for (int i = 0; i < 6; i++) { 
     tableRows[i] = new TableRow(this); 

    } 
    for (int i = 0; i < 6; i++) { 
     tableRows[0].addView(days[i]); 
    } 

    monthView.setBackgroundColor(Color.BLUE); 

    for (int i = 0; i < 6; i++) { 
     monthView.addView(tableRows[i]); 
    } 
} 
} 
1

に例を以下の翻訳あなたは、このようにしたいですか?

public TextView[] populateDays(int startDay) { 

    ... 

     for(int i = 0; i < 7; i++){ 
     ... 
     daysTextViewArr[i] = new TextView(this); 
     daysTextViewArr[i].setPadding(10, 0, 5, 0); 
     ... 
     } 

    ...