Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android TextView将活动的主题颜色作为其背景,而不是其背景图像_Android_Layout - Fatal编程技术网

Android TextView将活动的主题颜色作为其背景,而不是其背景图像

Android TextView将活动的主题颜色作为其背景,而不是其背景图像,android,layout,Android,Layout,我创建了一个活动并应用了橙色主题。我创建了一个自定义视图对象,其中包含一个背景图像和两个TextView对象。一个文本视图在左侧,另一个在右侧 我刚刚将该自定义视图作为活动的标题视图。 TextView的背景必须是该背景图像,但它显示的主题橙色作为其背景 我的代码: Header.java: package com.example.themes; import android.content.Context; import android.util.AttributeSet; import a

我创建了一个活动并应用了橙色主题。我创建了一个自定义视图对象,其中包含一个背景图像和两个TextView对象。一个文本视图在左侧,另一个在右侧

我刚刚将该自定义视图作为活动的标题视图。 TextView的背景必须是该背景图像,但它显示的主题橙色作为其背景

我的代码:

Header.java:

package com.example.themes;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

public class Header extends LinearLayout{

    public Header(Context context) {
        super(context);
        init();
    }

    public Header(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Header(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public void init(){
        LayoutInflater layInflator = (LayoutInflater)     getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        layInflator.inflate(R.layout.header_layout, this);
    }
}
header_layout.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" >

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/action_bar" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="left" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="right" />

</RelativeLayout>

你有多种方法可以做到这一点

使用android:scaleType=fitXY或

使用android:background=@drawable/image代替android:src

最好的方法是删除该ImageView并在RelativeLayout上应用android:background=@drawable/image


我要说的是,永远不要使用fitXY作为刻度类型。更好不过是中心作物。使用fitXY时,您可能会看到非常难看的图像。@Mike我同意,但对于普通背景图像,这应该不是问题。@anish-还请记住,当您在活动上应用主题时,活动的所有视图默认都使用该属性。如果大小和比率与承载图像的视图容器的大小相匹配,则是的。但一般来说,使用fitXY不是一个好主意。与拉伸图像相比,最好对图像进行适当的裁剪和显示。因此,在TextView中添加android:background=@null以从背景中删除主题。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.themes.MainActivity" >

    <com.example.themes.Header
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</RelativeLayout>
package com.example.themes;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}