【问题标题】:view rendered differently when I split the xml layout当我拆分 xml 布局时,视图呈现不同
【发布时间】:2011-12-31 21:59:30
【问题描述】:

我对 android 编程比较陌生,但对 java 并不陌生。

我一直在尝试设置一个重要的视图,但我遇到了一个奇怪的问题。我设计了一个可以水平滚动单个玩家面板的布局(它是另一个记分员)

所以我创建了一个带有 Horizo​​ntalScrollView 和 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>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    当我添加膨胀对象时,我需要指定正确的父对象而不是 null。一旦我这样做了,所有的 xml 属性都会进入视图。

    我能够使用层次结构查看器确认/调试这一点。

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

    需要

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

    【讨论】:

      【解决方案2】:

      阿卡梅斯瓦兰,

      造成这种差异的原因很简单:XML 是“静态”定义的,动态结果基于这些静态参数。也就是说,XML 使用由这些 XML 文件定义的初始状态来提供初始膨胀。该代码允许在运行时进行更改,并且即使位置和尺寸是恒定的,引擎也会将其视为“动态”。这为不熟悉 Android 的人带来了一些非常有趣的关系。

      当您拥有一个 XML 文件时,渲染引擎会在膨胀期间知道所有对象是什么以及它们与视图层次结构的关系。但是,当您拆分 XML 文件时,它不知道子视图的属性依赖于父视图的属性,除非您在代码中这样告诉它。本质上,这是因为计算机完全按照您的指示行事,如果您说得不够多,它就会发疯。它会“猜”,但几乎总是猜错。与 HTML 一样,Android 会尽可能地提供 A RESULT,即使它是错误的。

      Android 提供了几种方法来克服这个问题: OnLayout() 允许您动态地将定位应用到任何活动或视图对象。 OnMeasure() 允许您重新定义尺寸。 OnDraw() 允许您动态绘制或膨胀对象。通过覆盖这些函数,您可以将行为恢复到您需要的状态,并且仍然保持 XML 模块化,就像您希望的那样。由于您使用的是标准化的 API 控件,因此您可能希望直接在 Activity 中覆盖这些函数,从而省去很多麻烦。

      希望这会有所帮助, 模糊逻辑

      【讨论】:

      • 感谢您的意见,但是,我找到了根本原因。当我添加膨胀的 .xml 时,我没有指定父级,这导致我的“match_parent”属性不被应用。这记录在 UI 中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      • 2014-08-18
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多