1
我已創建一個活動並使用橙色的應用主題。我創建了一個包含背景圖像和2個TextView對象的自定義View對象。 1 TextView在左側,另一個在右側。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>
activity_main.xml中:
<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>
MainActivity.java:
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);
}
}
我說從來沒有使用fitXY爲規模型。更好的是centerCrop。用fitXY你可能會看起來非常笨拙的圖像。 – Mike 2014-10-01 08:54:04
@Mike我同意,但對於普通的背景圖片,它不應該是一個問題。 – 2014-10-01 09:01:32
@anish - 還要記住,當您在活動上應用主題時,活動的所有視圖默認都會使用該屬性。 – 2014-10-01 09:02:41