【发布时间】:2011-12-31 21:59:30
【问题描述】:
我对 android 编程比较陌生,但对 java 并不陌生。
我一直在尝试设置一个重要的视图,但我遇到了一个奇怪的问题。我设计了一个可以水平滚动单个玩家面板的布局(它是另一个记分员)
所以我创建了一个带有 HorizontalScrollView 和 LinearLayout 的 playermain.xml 来包含动态添加的播放器面板,它们是一个 TableLayout。
我希望播放器面板按钮和字段可以拉伸以垂直占用所有可用的游戏空间(我也有一些水平目标,但我们暂时忽略这些目标)
我尝试过的任何方法都不会让播放面板伸展 - 相反它们居中,但不填充。奇怪的是,我尝试了一个快速测试,即创建 xml 文件的扁平版本(即,我将 playerpanel xml 的两个副本复制到主 xml 中。当我简单地将 setContentView 设置为这个组合的 xml 时,我得到了视图我要。
当我以编程方式执行此操作时,我没有得到伸展,而且我一定在这里遗漏了一些东西。我也可以发布组合的 xml,但我认为没有必要,基本上当我创建一个带有 Scrolls、Layout 和 tablelayouts 的单个 xml 文件时,事情会按照我想要的方式展开。当我使用一个 xml 文件创建滚动/布局,然后将其他项目添加到顶级 LinearLayout 时,它们不会拉伸。
创建:
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);
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>
【问题讨论】: