一、getWindowManager().getDefaultDisplay().getHeight() 和getWindowManager().getDefaultDisplay().getWidth()被移除The method getHeight() is deprecated,替代方法。
1.Activity中
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
SCREEN_WIDTH = dm.widthPixels;
SCREEN_HEIGHT = dm.heightPixels;
2.Adaptor和Fragment中
(getWindowManager()方法是在类Activity中的)
DisplayMetrics dm = getResources().getDisplayMetrics();
layout.setLayoutParams(new LinearLayout.LayoutParams(dm2.widthPixels,dm2.heightPixels));
<?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:id="@+id/layout_main"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="显示详细信息" />
</LinearLayout>
</LinearLayout>
package com.example.wxt.fragmentdy;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
Button button;
FragmentManager manager;
LinearLayout layout;
//静态添加Fragment直接在activity中添加Fragment,动态添加为在MainActivity中调用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
layout=findViewById(R.id.line1);
manager= getSupportFragmentManager();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
layout.setLayoutParams(new LinearLayout.LayoutParams(dm.widthPixels/3,dm.heightPixels));
FragmentTransaction transaction=manager.beginTransaction();
transaction.replace(R.id.layout_main,new MyFragment());//replace()和add()均可
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="#ffff00"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="60dp"
android:layout_marginBottom="188dp"
android:text="隐藏"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
package com.example.wxt.fragmentdy;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyFragment extends Fragment {
LinearLayout layout;
Button button;
FragmentManager manager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
layout = getActivity().findViewById(R.id.line1);
View view = inflater.inflate(R.layout.layout1,container,false);
button= view.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.remove(MyFragment.this);
transaction.commit();
maxScreen();
}
});
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
maxScreen();
}
public void maxScreen(){
DisplayMetrics dm2 = getResources().getDisplayMetrics();
layout.setLayoutParams(new LinearLayout.LayoutParams(dm2.widthPixels,dm2.heightPixels));
}
}
三、ToolBar
1.取消标题显示
styles.xml中windowNoTitle-True
package com.example.wxt.toolbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
toolbar.setLogo(R.mipmap.ic_launcher);//设置logo
toolbar.setTitle("主");//设置主标题,副标题
toolbar.setSubtitle("子");
MainActivity.this.setSupportActionBar(toolbar);//toolbar支持菜单
toolbar.setNavigationIcon(R.mipmap.ic_launcher);//设置返回标志
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {//单击显示菜单
@Override
public boolean onMenuItemClick(MenuItem item) {
String str = null;
switch(item.getItemId())
{
case R.id.item1:
str="您选择了菜单1";
break;
case R.id.item2:
str="您选择了菜单2";
break;
case R.id.item3:
str="您选择了菜单3";
break;
}
Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" >
<Button
android:id="@+id/button"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="B"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="4dp" />
<TextView
android:textSize="20dp"
android:padding="10dp"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页" />
</android.support.v7.widget.Toolbar>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item1"
android:title="编辑1"
android:icon="@mipmap/ic_launcher"
app:showAsAction="never"
></item>
<item
android:id="@+id/item2"
android:title="编辑2"
android:icon="@mipmap/ic_launcher"
app:showAsAction="always"
></item>
<item
android:id="@+id/item3"
android:title="编辑3"
android:icon="@mipmap/ic_launcher"
app:showAsAction="never"
></item>
</menu>