之前在网上看到一个一种方法:
public class BaseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.base); } public void baseSetContentView(int layoutResId) { LinearLayout llContent = (LinearLayout) findViewById(R.id.content); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(layoutResId, null); llContent.addView(v); } }
经过测试,用这种方法加进去的view都是wrap_content的
需要改成inflate.inflate(layoutResId,llContent);
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub setTheme(R.style.MyTheme);//注意这一句的位置 super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_basechart); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title); } public void baseSetContentView(int layoutResId) { LinearLayout llContent = (LinearLayout) findViewById(R.id.baseContent); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(layoutResId, llContent); }
activity_basechart.xml
<?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" android:orientation="vertical" android:background="#FFFFFF"> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="2dp" android:orientation="vertical" android:layout_weight="1" android:padding="2dp" android:background="@drawable/border"> <com.github.mikephil.charting.charts.LineChart android:id="@+id/chart" android:background="@color/tp" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:id="@+id/baseContent" android:layout_weight="1"> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical" android:layout_weight="1"> <android.support.v4.view.ViewPager android:id="@+id/baseVallery" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> </LinearLayout> </LinearLayout>
新建Activity继承自上面的BaseActivity
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); baseSetContentView(R.layout.activity_monitor); }