在我的主要活动中,我有一个工具栏。我也有一个自定义列表视图的布局。我想在这里展示工具栏。但无论我做什么我都不能。问题是什么?我想在我的主要活动中显示相同的工具栏。
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>
答案 0 :(得分:0)
我认为您的ListView
和Toolbar
布局相互重叠,这就是您无法看到的原因。如果您想在工具栏下方显示ListView
,则必须在android:layout_below="@+id/toolbar"
中设置ListView
属性
<?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>