2011-12-30 4 views
0

私はAndroidプログラミングにはかなり新しいですが、Javaでは新しいものではありません。xmlレイアウトを分割したときに表示が異なって表示される

私は些細なビューをセットアップしようとしていましたが、私は奇妙な問題を抱えています。

私はHorizo​​ntalScrollViewとLinearLayoutでplayermain.xmlを作成し、動的に追加されたTableLayoutのプレーヤパネルを含むようにレイアウトしました。

私が試した何が再生パネルを伸ばすために取得しません

(私も水平方向のためのいくつかの目標を持っていますが、今のところ、それらを無視してみましょう)垂直使用可能なすべての演奏スペースを消費するためにストレッチするplayerpanelボタンやフィールドをしたいです代わりに中心になりますが、一杯になりません。奇妙なことですが、私はxmlファイルのフラット化されたバージョンを作成することでした(つまり、私はplayerpanel xmlの2つのコピーをメインXMLにコピーしました。

プログラマチックに行うと、ストレッチが得られず、ここで何かが欠落しているはずです。また、結合されたxmlを投稿することもできますが、基本的にはxml 1つのxmlファイルでスクロール/レイアウトを作成した後、他のアイテムをトップレベルのLinearLayoutに追加すると、伸縮しません。

onCreate: < ---- SNIP ----->

setContentView(R.layout.playingmain); 
    final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout llayout = (LinearLayout)findViewById(R.id.layout1); 


     TableLayout tl = (TableLayout)inflater.inflate(R.layout.playerpanel, null); 
     InitializePlayer(player1,tl); 
     llayout.addView(tl); 

< ---- SNIP ----->

MAINPANEL.XML

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbars="horizontal" 


    > 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/layout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:isScrollContainer="true" 
     android:scrollbarAlwaysDrawHorizontalTrack="true" 
     android:gravity="center" 
     > 
    </LinearLayout> 

</HorizontalScrollView> 

個々Panel.xml:

<TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:paddingLeft="5sp" 
     android:stretchColumns="*" 
     android:minWidth="200sp" 
     > 

     <TableRow 
      android:id="@+id/NameRow" 
      android:minWidth="500sp" 
      android:layout_weight = "1" 
      > 

      <TextView 
       android:id="@+id/PlayerName" 
       android:text="PlayerName" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 

       /> 

      <TextView 
       android:id="@+id/dbg1" 
       android:text="TextView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 

       /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/scoreRow" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight = "1" 
      > 

      <TextView 
       android:id="@+id/currentScore" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="10" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       /> 

      <TextView 
       android:id="@+id/dbg2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="TextView" 
       /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/TableRow17" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight = "1" 
      > 

      <Button 
       android:id="@+id/subtractButton" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="-" 
      /> 

      <EditText 
       android:id="@+id/scoreEntry" 
       android:layout_span = "2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:inputType="number" /> 

      <Button 
       android:id="@+id/addButton" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="+" 
       /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight = "1" 
      > 

      <Button 
       android:id="@+id/plusOne" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="+1" /> 

      <Button 
       android:id="@+id/plusFive" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="+5" /> 

      <Button 
       android:id="@+id/minusOne" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="-1" /> 

      <Button 
       android:id="@+id/minusFive" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="-5" /> 

     </TableRow> 

    </TableLayout> 

答えて

0

Akameswaran、

この不一致の理由は簡単です:XMLは "statiこれらの静的パラメタに基づいた動的結果で定義されています。つまり、XMLは、これらのXMLファイルで定義された初期状態を使用して初期インフレーションを提供します。このコードは、実行時に変更を行うことを可能にし、位置と寸法が一定であっても、エンジンによって「動的」として扱われます。これは、Androidに慣れていない人にとっては非常に興味深い関係につながります。

XMLファイルが1つある場合、インフレ時にレンダリングエンジンは、すべてのオブジェクトが何であるか、それらがビュー階層にどのように関連しているかを認識します。ただし、XMLファイルを分割すると、子ビューのプロパティが親ビューのプロパティに依存していることが分かりません。これは、本質的には、コンピュータが正確にあなたがそれを伝え、あなたがそれを十分に伝えていないならば、狂ってしまうからです。それは「推測」されますが、ほとんどの場合、間違って推測されます。 HTMLのように、Androidはできるだけ多くのことを行い、間違っている場合でも可能な限りRESULTを提供します。

Androidはこれを解決するいくつかの方法を提供しています:OnLayout()は、任意のアクティビティまたはビューオブジェクトに位置を動的に適用することを可能にします。 OnMeasure()を使用すると、ディメンションを再定義できます。 OnDraw()を使用すると、オブジェクトを動的に描画または膨張させることができます。これらの関数をオーバーライドすることで、必要なものに振舞いを戻すことができ、必要に応じてXMLモジュラーを維持できます。標準化されたAPIコントロールを使用しているので、おそらくアクティビティで関数を直接オーバーライドして、多くの問題を解決したいと思うでしょう。このことができます

希望、

+0

のおかげであることを必要と

TableLayout tl = (TableLayout)inflater.inflate(R.layout.playerpanel, null); 

、しかし、私は、根本的な原因を突き止め。私が親を指定していなかった膨らんだ.xmlを追加すると、これは私の "match_parent"プロパティが適用されない原因となった。これはUIに文書化されています – akameswaran

1

私が膨張したオブジェクトを追加 FuzzicalLogicは、私がnullの代わりに適切な親オブジェクトを指定する必要がありました。私がそれをしたら、すべてのxmlプロパティはそれをビューにしました。

これは階層ビューアを使用して確認/デバッグできました。入力用

TableLayout tl = (TableLayout)inflater.inflate(R.layout.playerpanel, llayout, false); 
関連する問題