将我的主 activity 工具栏添加到另一个布局
add my main activity toolbar to another layout
在我的主 activity 中有一个工具栏。我也有一个带有自定义列表视图的布局。我也想在这里显示该工具栏。但无论我做什么我都做不到。问题是什么?我希望我的主 activity 中的相同工具栏也显示在这里。
About.java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class About extends AppCompatActivity{
ListView list;
String[] text = {
"Version"
} ;
Integer[] imageId = {
R.drawable.ic_info_black_24dp
};
private Toolbar toolbar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
// Set a Toolbar to replace the ActionBar. (doesn't work)
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CustomList adapter = new
CustomList(About.this, text, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(About.this, "You Clicked at " + text[+position], Toast.LENGTH_SHORT).show();
}
});
}
}
about.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">
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:background="@drawable/about_background">
</ListView>
</RelativeLayout>
我认为您的 ListView
和 Toolbar
布局彼此重叠,这就是您看不到的原因。如果你想在工具栏下面显示 ListView
那么你必须在 ListView
中设置 android:layout_below="@+id/toolbar"
属性 像那样
<?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">
<include
layout="@layout/toolbar"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_below="@+id/toolbar
android:background="@drawable/about_background">
</ListView>
</RelativeLayout>
在我的主 activity 中有一个工具栏。我也有一个带有自定义列表视图的布局。我也想在这里显示该工具栏。但无论我做什么我都做不到。问题是什么?我希望我的主 activity 中的相同工具栏也显示在这里。
About.java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class About extends AppCompatActivity{
ListView list;
String[] text = {
"Version"
} ;
Integer[] imageId = {
R.drawable.ic_info_black_24dp
};
private Toolbar toolbar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
// Set a Toolbar to replace the ActionBar. (doesn't work)
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CustomList adapter = new
CustomList(About.this, text, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(About.this, "You Clicked at " + text[+position], Toast.LENGTH_SHORT).show();
}
});
}
}
about.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">
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:background="@drawable/about_background">
</ListView>
</RelativeLayout>
我认为您的 ListView
和 Toolbar
布局彼此重叠,这就是您看不到的原因。如果你想在工具栏下面显示 ListView
那么你必须在 ListView
中设置 android:layout_below="@+id/toolbar"
属性 像那样
<?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">
<include
layout="@layout/toolbar"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_below="@+id/toolbar
android:background="@drawable/about_background">
</ListView>
</RelativeLayout>