Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

android - How to add background image with Linear Layout as a root layout and having nested Linear Layout

I am trying to add background image which has linear layout as a root layout [Look hierarchy diagram for reference.] I have tried using Relative Layout as a root layout but it overlap the nested linear layouts. While with linear layout as a root layout it displays only at the bottom.

XML Code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<LinearLayout
    android:id="@+id/line1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:textSize="26sp"
            android:textStyle="bold"
            android:text="Team A" />

        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:text="0"
            android:textSize="40sp"
            android:textStyle="bold"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:onClick="add3forA"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="+3 Points" />


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:onClick="add2forA"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:onClick="add1forA"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="Free Throw" />
    </LinearLayout>

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:textSize="26sp"
            android:textStyle="bold"
            android:text="Team B" />

        <TextView
            android:id="@+id/team_b_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="40sp"
            android:textStyle="bold"
            android:text="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:onClick="add3forB"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="+3 Points" />


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:onClick="add2forB"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:onClick="add1forB"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="Free Throw" />
    </LinearLayout>

</LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reset"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="40dp"
        android:onClick="reset"/>
</LinearLayout> 

View Hierarchy Diagram

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/image">

    <LinearLayout
        android:id="@+id/line1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:textSize="26sp"
                android:textStyle="bold"
                android:text="Team A" />

            <TextView
                android:id="@+id/team_a_score"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:text="0"
                android:textSize="40sp"
                android:textStyle="bold"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:onClick="add3forA"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text="+3 Points" />


            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:onClick="add2forA"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text="+2 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:onClick="add1forA"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text="Free Throw" />
        </LinearLayout>

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:textSize="26sp"
                android:textStyle="bold"
                android:text="Team B" />

            <TextView
                android:id="@+id/team_b_score"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textSize="40sp"
                android:textStyle="bold"
                android:text="0" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:onClick="add3forB"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text="+3 Points" />


            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:onClick="add2forB"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text="+2 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:onClick="add1forB"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text="Free Throw" />
        </LinearLayout>

    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reset"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="40dp"
        android:onClick="reset"/>
</LinearLayout>

android:background="@drawable/image" Add this line in your Root Layout


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...