0

次のレイアウトファイルがあります。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/blue" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/yellow" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/blue" />

</LinearLayout>

ネストされた重みを使用しており、パフォーマンスの問題を認識しています。
このレイアウトをアプリウィジェットに表示したいのですが、アプリウィジェットをロードすると、「ウィジェットをロードできませんでした」というメッセージが表示されます。これは、ネストされた重みが原因だと思います(わかりません)。

基本レイアウトにを使用するRelativeLayoutと、次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/blue" />

    <View
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/blue" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom"
        android:layout_below="@id/top" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/yellow" />
    </LinearLayout>

</RelativeLayout>

このレイアウトを使用すると、赤と黄色のビューが表示されません。高さはにとどまっていると思います0dp

どうすれば目標を達成できますか?
明確にするために:私は上下の固定高さでビューを表示したいと思います。それらの間に、ギャップを埋めて、同じ高さのビューをいくつか表示したいと思います。


編集
だから私は何かを見つけました。レイアウトに2つのレベルのViewGroupがある場合、appwidgetは次のように表示されません。

<LinearLayout
    android:id="@+id/middle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:orientation="vertical" >


</LinearLayout>

<View
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentTop="true"
    android:background="@color/teal" />

<View
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentBottom="true"
    android:background="@color/teal" />

これは機能しますが、機能しません。

<LinearLayout
    android:id="@+id/middle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red" />
</LinearLayout>

<View
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentTop="true"
    android:background="@color/teal" />

<View
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentBottom="true"
    android:background="@color/teal" />

なぜこれが起こるのか、そして私のアイデアを実装する方法を誰かが知っていますか?クリストファーの画像は私の考えを示しています。

4

2 に答える 2

0

私があなたの要求を正しく理解しているなら、これはあなたが達成しようとしているレイアウトです。

レイアウト

このレイアウトを作成するためにxmlコードを変更しました。ここにあります:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white" />
    </LinearLayout>

    <View
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_alignParentTop="true"
        android:background="@color/teal" />

    <View
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_alignParentBottom="true"
        android:background="@color/teal" />

</RelativeLayout>

重みがすべて1に設定されている場合、「中央」に配置されたすべてのビューの高さは均一になります。

于 2012-07-19T20:36:16.427 に答える
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:id="@+id/theparent"
>
<View 
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentTop="true"
    android:background="@color/red"
    />



<View 
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentBottom="true"
    android:background="@color/red"
    />
</RelativeLayout>

これでヘッダーとフッターの面倒を見ることができます。コンテンツ領域で何をしたいのか詳しく教えてください。お手伝いできるかもしれません。

于 2012-07-19T20:44:25.443 に答える