2

我有一个chart.xml并且我在其中设置了一个背景图表ViewPagerusing android:background="@drawable/chart_bg"

问题是:

当我看到它的布局模式时, xml 视图显示,chart_bg但是当我启动/运行应用程序时chart_bg,在真实设备中看不到背景图表。

图表.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/chartmain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >        

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="1dp"
            android:gravity="center_horizontal"
            android:layout_gravity="center_horizontal" >

            <TextView
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="fill_horizontal"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:text="Fri Jul 13, 2012"
                 />

            </LinearLayout>

        <RelativeLayout
            android:id="@+id/relative"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
             >

            <android.support.v4.view.ViewPager
                android:id="@+id/HView"
                android:layout_width="360dp"
                android:layout_height="150dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="15dp"
                android:background="@drawable/chart_bg">


            </android.support.v4.view.ViewPager>
        </RelativeLayout>
   </LinearLayout>

    </LinearLayout>

Java 文件:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        Bundle bundle = getArguments();

        view = inflater.inflate(R.layout.chart, container, false);


            return view;

            }
4

1 回答 1

9

最后通过在代码中显式设置背景图像来解决,如下所示:

private ViewPager mPager;
mPager = (ViewPager) view.findViewById(R.id.HView);
mPager.setBackgroundResource(R.drawable.chart_bg);

谢谢!

于 2013-06-10T07:52:20.273 回答