【问题标题】:Android my LinearLayoutAndroid 我的线性布局
【发布时间】:2011-09-15 19:23:46
【问题描述】:

我扩展 LinearLayout 并创建自己的类 GameLinerLayout,然后创建类 LudoGame 扩展该类 GameLinerLayout

现在我尝试在我的布局“main.xml”中使用它

<android.game.ludo.LudoGame xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/ll_absolute"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@drawable/background">
</android.game.ludo.LudoGame>

但我不能让它工作“背景”参数。那么如何在我自己的视图上设置背景图片呢?

我不明白这一点。我有我的布局,我只想在该视图上设置背景(没有其他视图),如果我这样做

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/wood">
<android.game.ludo.LudoGame xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_absolute"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</android.game.ludo.LudoGame>
 </LinearLayout>

它不显示任何内容,如果我在“LudoGame”布局上设置“android:background="#FFFFFF"”,那么我会看到内容但没有背景。你能告诉我一些如何解决这个问题的例子吗?

谢谢。

这与我的布局工作方式相同 GameLinerLayout 扩展 LinearLayout 我@Override onDraw 方法在哪里是对象 Canvas 以及我在哪里绘制内容

@Override
protected void onDraw(Canvas canvas) 
{
    super.dispatchDraw(canvas);
    drawFields(canvas);
}

但我不想用 Canvas 绘制背景图像,我想(如果可能的话)在我尝试定义(使用)我的布局时在 XML 中设置该背景

【问题讨论】:

    标签: android


    【解决方案1】:

    在其中采用另一个线性布局。它会起作用的。因为你还没有在你的扩展类中定义任何背景属性,所以最好在里面放一个带有fill_parent宽度和高度的线性布局!!

    【讨论】:

    • 我尝试过,但是我用画布绘制的“android.game.ludo.LudoGame”中的所有内容都消失了(新的 linerLayoout 已经结束了)。如何在扩展类中添加该属性?我可以使用画布设置该背景,但每次刷新视图时,它都会刷新该背景。谢谢
    • 只用你的扩展布局创建另一个xml文件,并添加你想在你的主文件上操作的视图。只需将它们添加到那个xml文件中,所以它都是那个扩展的线性布局的子视图然后你就可以随心所欲地使用它们了..
    • 我发现您不需要在该布局的类中设置属性。您可以通过在 xml 文件中设置背景轻松地做到这一点,它有时会发生在您将您的子视图作为宽度和高度的 fill_parent 时。
    【解决方案2】:

    试试这个:我认为它必须有效。

    <android.game.ludo.LudoGame xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ll_absolute"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/wood">
         </LinearLayout>
    </android.game.ludo.LudoGame>
    

    为此扩展布局创建另一个 xml 文件。

     <android.game.ludo.LudoGame xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/ll_absolute"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"> 
    // now add all the views(not layouts) here, u want to create them with same id in both file
    </android.game.ludo.LudoGame >
    

    希望您能理解并帮助您...

    【讨论】:

    • 也许我错误地定义了我需要的东西,所以这就是我的情况。我有我的新 LinerLayout,我在其中使用 Canvas 绘制形状、图像、矩形,我无法在我的布局中设置背景,我知道的唯一方法是在我的布局中的所有内容之前绘制图像(使用画布) .因此,当我使用我的布局时,无论我稍后在 XML 中添加的任何视图,它都会覆盖我的布局,也会覆盖我在其中绘制的形状。所以也许解决方案是我需要在我的布局中设置该背景,而不是添加一些其他带有背景的布局?
    • 没有。游戏是不是没有“拖放”的可能性,所有游戏内容都在我的布局中。
    • 我也有点不自信,我不明白你想做什么,但发现你可以通过编程扩展布局及其属性,试试这个链接。 stackoverflow.com/questions/3885077/…
    • 那么不要把子视图属性fill_parent,把它们放在wrap_content上,然后你的扩展布局背景不会被它的子视图重叠。我希望它一定能奏效。
    猜你喜欢
    • 1970-01-01
    • 2014-11-21
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    相关资源
    最近更新 更多