Android 自定义操作栏与其他元素重叠
我正在尝试在活动中实现自定义操作栏。我在下面已经这样做了,但是我的自定义操作栏与另一个重叠了。我已附上下面的屏幕截图Android 自定义操作栏与其他元素重叠,android,android-actionbar,Android,Android Actionbar,我正在尝试在活动中实现自定义操作栏。我在下面已经这样做了,但是我的自定义操作栏与另一个重叠了。我已附上下面的屏幕截图 public class MainActivity extends ActionBarActivity { private TextView actionBarTitle; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-gene
public class MainActivity extends ActionBarActivity {
private TextView actionBarTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getSupportActionBar();
View customView=getLayoutInflater().inflate(R.layout.actionbar_layout, null);
actionBar.setCustomView(customView);
actionBarTitle =(TextView)findViewById(R.id.actionbar_text);
actionBarTitle.setText("Action bar");
}
}
舱单:
<application
android:name="MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme"
android:requiredForAllUsers="false">
<activity android:name="MainActivity"
android:screenOrientation="portrait"/>
</application>
actionbar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >
<ImageView
android:id="@+id/actionbar_icon"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/actionbar_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_toRightOf="@+id/actionbar_icon"
android:text="title" />
</RelativeLayout>
strings.xml:
<resources>
<string name="app_name">sample</string>
</resources>
样品
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="Dialog_No_Border">
<item name="android:windowIsFloating">true</item>
<item name="android:windowBackground">@color/transparent_color</item>
</style>
</resources>
符合事实的
@颜色/透明颜色
如何删除示例文本和黑色背景,以便只显示自定义视图
您在屏幕截图中显示的内容正是规范所说的应该发生的。在操作栏中使用自定义视图时,它位于操作栏左侧的应用程序图标(如果有)和操作栏右侧的操作按钮(如果有)之间 自定义导航视图显示在应用程序图标和任何操作按钮之间,并且可能会使用其中的任何可用空间
尝试从活动类扩展并使用Actionbar
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
**actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);**
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText("your title here");
actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);
这对我有用!通过膨胀自定义操作栏!但要确保它不会妨碍其他布局或组件
ps:我在我的一个项目中使用过这个,效果很好,如果您遇到任何错误/错误/建议,请纠正我。如何删除示例文本和黑色背景…因此只显示自定义视图…(示例)文本和黑色条必须消失,并且只有可视图标和操作栏文本。。。。。