2011-07-17 8 views
1

上のボタンのテキストの配置を変更する>これは私のmain.xmlです: `アンドロイド - 相対的なレイアウトは、クリック

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:addStatesFromChildren="false" 
    android:layout_height="wrap_content"> 
    <TableLayout 
     android:id="@+id/header" 
     android:layout_marginRight="2dp" 
     android:layout_marginLeft="2dp" 
     android:layout_height="wrap_content" 
     android:background="@android:color/black" 
     android:layout_width="fill_parent" 
     android:layout_alignParentTop="true"> 
     <TableRow> 
      <TextView 
       android:id="@+id/leftHeader" 
       android:textColor="@android:color/black" 
       android:layout_margin="1dp" 
       android:gravity="center_horizontal" 
       android:layout_weight="1" 
       android:background="#ffcccccc" 
       android:text="textview1" 
       android:layout_width="30dip" /> 
      <TextView 
       android:id="@+id/rightHeader" 
       android:textColor="@android:color/black" 
       android:gravity="center_horizontal" 
       android:layout_weight="1" 
       android:layout_margin="1dp" 
       android:background="@android:color/white" 
       android:text="textview2" 
       android:layout_width="70dip" /> 
     </TableRow> 
    </TableLayout> 
<ScrollView 
    android:id="@+id/table_scroll" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="2dp" 
    android:layout_marginLeft="2dp" 
    android:layout_below="@+id/header" 
    android:layout_above="@+id/buttonTable"> 
    <TableLayout 
     android:id="@+id/maintable" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/black"> 
     <TableRow 
      android:id="@+id/maintableRow"> 
      <TextView 
       android:id="@+id/maintableLeft" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:layout_marginRight="1dp" 
       android:layout_marginLeft="1dp" 
       android:text="textview1" 
       android:layout_width="30dip" /> 
      <TextView 
       android:id="@+id/maintableRight" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:layout_marginLeft="1dp" 
       android:layout_marginRight="1dp" 
       android:text="textview2" 
       android:layout_width="70dip" /> 
     </TableRow> 
    </TableLayout> 
</ScrollView> 
<TableLayout 
    android:id="@+id/buttonTable" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/black" 
    android:layout_alignParentBottom="true"> 
    <TableRow> 
     <Button 
      android:id="@+id/button_ResetData" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:text="Reset Data" 
      android:layout_gravity="center" 
      android:layout_weight="1" /> 
     <Button 
      android:id="@+id/button_Refresh" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:text="Refresh" 
      android:gravity="center" 
      android:layout_weight="1" /> 
    </TableRow> 
</TableLayout> 

`

私はそのテキストの配置が行くのいずれかのボタンを押した後、左に、どのようにして中心に留めることができますか? 最初のテーブル(ヘッダー)(textview1、textview2)でも同様のことが起こりますが、ここではテキストのサイズをテキストサイズで再設定します。

EDIT:変更の原因となるコードを見つけることができましたしかし、私はこれを修正する方法を知らない。 なぜこれが起こり、どうやって解決するのか、私に何人か説明できますか?

private void fillTableView(List<BatteryInfo> list) { 

    TextView leftHeader = (TextView) findViewById(R.id.leftHeader); 
    TextView rightHeader = (TextView) findViewById(R.id.rightHeader); 

    LayoutParams leftHeaderLayoutParams = leftHeader.getLayoutParams(); 
    LayoutParams rightHeaderLayoutParams = rightHeader.getLayoutParams(); 

    TableLayout contentTable = (TableLayout) findViewById(R.id.maintable);  

    contentTable.removeAllViews(); 

    for (int i = list.size() - 1; i >= 0; i--) { 

     TextView tv1 = new TextView(this); 
     tv1.setTextColor(Color.BLACK); 
     tv1.setGravity(Gravity.CENTER); 
     tv1.setBackgroundColor(Color.LTGRAY); 
     tv1.setLayoutParams(leftHeaderLayoutParams); 
     tv1.setText(String.valueOf(list.get(i).getLevel())); 

     TextView tv2 = new TextView(this); 
     tv2.setTextColor(Color.BLACK); 
     tv2.setGravity(Gravity.CENTER); 
     tv2.setBackgroundColor(Color.WHITE); 
     tv2.setLayoutParams(rightHeaderLayoutParams); 
     tv2.setText(list.get(i).getDurationTimeInString()); 

     TableRow tblRow = new TableRow(this); 
     tblRow.addView(tv1); 
     tblRow.addView(tv2); 

     contentTable.addView(tblRow);     
    }  
} 

removeAllViewsまたは任意のaddView関数を使用した後、前に説明したように、一番上のテーブルのレイアウトが変更されます。なぜこれが起こるのですか?

+0

が実行されている他のコードはありますか?私はこのレイアウトをスクラッチ・アプリケーションに貼り付けて実行しました。ボタンをクリックすると、ボタンのテキストの配置が変更されていないように見えます。 –

+0

はい、そうです。私はそれを加えました、見てください –

+0

私はあなたのレイアウトに 'leftHeader'または 'rightHeader'と呼ばれるものは何も表示されません。あなたは完全なレイアウトを投稿できますか? –

答えて

1

あなたはandroid:layout_width="fill_parent"leftHeaderrightHeaderandroid:layout_width="30dip"android:layout_width="70dip"を変更することでこの問題を解決することができます。私はそれがなぜ、どういうわけか私は完全にはっきりしていないと認めなければならない。

毎回TableLayoutをクリアして再描画する代わりに、ListAdapterを使用することを検討してください。それは、あなたがやっていることにはるかに適しています(そして、おそらくもっと良くなるでしょう)。

更新番号2:

お知らせmain.xmlの変化とのonCreateメソッドの変更。これは、あなたのリストビューをスクロールさせながらあなたのヘッダーを静的にし、あなたのボタンのTableLayoutからLinearLayoutに変更することで配置の問題が解決されます。ここで

が(本当にこのような動的なデータのためのものではありません)の代わりにTableLayoutのカスタムアダプタでこれを行う方法は次のとおりです。あなたはこれにmain.xml

変更:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:addStatesFromChildren="false" android:layout_height="wrap_content"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" android:id="@+id/header"> 
<TextView android:id="@+id/leftHeader" android:textColor="@android:color/black" 
    android:layout_margin="1dp" android:gravity="center_horizontal" 
    android:layout_weight="1" android:background="#ffcccccc" android:text="textview1" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"/> 
<TextView android:id="@+id/rightHeader" android:textColor="@android:color/black" 
    android:gravity="center_horizontal" android:layout_weight="1" 
    android:layout_margin="1dp" android:background="@android:color/white" 
    android:text="textview2" android:layout_width="fill_parent" android:layout_height="wrap_content"/> 
</LinearLayout> 

<LinearLayout android:id="@+id/buttonTable" android:orientation="horizontal" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:background="@android:color/black" 
    android:layout_alignParentBottom="true" > 
     <Button android:id="@+id/button_ResetData" 
      android:layout_height="wrap_content" android:layout_width="fill_parent" 
      android:text="Reset Data" android:layout_gravity="center" 
      android:layout_weight="1" /> 
     <Button android:id="@+id/button_Refresh" 
      android:layout_height="wrap_content" android:layout_width="fill_parent" 
      android:text="Refresh" android:gravity="center" 
      android:layout_weight="1" /> 
</LinearLayout> 

<ListView android:id="@+id/listView" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:layout_weight="1" 
    android:fadingEdge="none" 
    android:layout_below="@id/header" android:layout_above="@id/buttonTable"> 
</ListView> 

あなたの行(row.xml)のためのXMLレイアウトファイルを追加します。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    <TextView android:id="@+id/maintableLeft" 
     android:layout_height="wrap_content" android:layout_weight="1" 
     android:layout_marginRight="1dp" android:layout_marginLeft="1dp" 
     android:gravity="center_horizontal" android:background="#ffcccccc" 
     android:textColor="@android:color/black" android:layout_width="fill_parent" /> 
    <TextView android:id="@+id/maintableRight" 
     android:layout_height="wrap_content" android:background="#ffcccccc" 
     android:textColor="@android:color/black" android:layout_weight="1" 
     android:layout_marginLeft="1dp" android:layout_marginRight="1dp" 
     android:gravity="center_horizontal" android:layout_width="fill_parent" /> 
</LinearLayout> 

は今、すべてがあなたの情報を表示する(そしてより良いあなたのレイアウト作業を行う)ためにBaseAdapterからカスタムクラスを作成する場所にあります。

class CustomAdapter extends BaseAdapter { 
    private List<BatteryInfo> info; 
    private LayoutInflater inflater; 

    public CustomAdapter(Context context, List<BatteryInfo> x) { 
     this.info = x; 
     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return info.size(); 
    } 

    public Object getItem(int position) { 
     return info.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     View v; 
     v = inflater.inflate(R.layout.row, parent, false); 

     BatteryInfo b = (BatteryInfo) getItem(position); 

     ((TextView) v.findViewById(R.id.maintableLeft)).setText(b 
       .getDurationTimeInString()); 
     ((TextView) v.findViewById(R.id.maintableRight)).setText(b 
       .getLevel()); 

     return v; 
    } 
} 

は、それを追加し、ヘッダを膨らませるために、あなたのonCreateメソッドを変更して、あなたのデータをバインド新しいCustomAdapterを(私はこの例のための偽のBatteryInfoクラスを作成しました、あなたはこの作業を取得するために少し物事を変更する必要があります)を使用して、リストビューに:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final ListView lv = (ListView) this.findViewById(R.id.listView); 
    final Context context = this; 

    Button refresh = (Button) this.findViewById(R.id.button_Refresh); 
    refresh.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      List<BatteryInfo> x = new ArrayList<BatteryInfo>(); 
      x.add(new BatteryInfo()); 
      x.add(new BatteryInfo()); 
      x.add(new BatteryInfo()); 
      x.add(new BatteryInfo()); 


      lv.setAdapter(new CustomAdapter(context, x)); 
     } 
    }); 
} 
+0

"fill_parent"に設定すると、2つのテーブルの幅の値が異なります。私は各列の幅の値が同じになる必要があります。どうすればいい? –

+0

これはどのように動作するのか分かりません。リストビューに新しいリストを追加することを前提としていますが、表示できるのは ((TextView)v.findViewById(R.id.maintableLeft)))setText(b .getDurationTimeInString()); ((TextView)v.findViewById(R.id.maintableRight))。setText(b .getLevel()); すべてのリストはどこですか? –

+0

わかりません。それがうまくいかない、あるいはうまくいくと言っていますが、それがなぜ機能するのか分からないのですか? –

関連する問題