【问题标题】:Android Actionbar buttons stretched?Android Actionbar 按钮被拉伸?
【发布时间】:2023-11-07 03:05:01
【问题描述】:

在我的 android 操作栏中,我有 2 个自定义按钮,我会在下面发布代码,但我的问题是按钮在操作栏中被拉伸,如何将其设置为适合文本而不是拉伸?这是我的代码

activity_main_ab.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="fill_horizontal"
android:orientation="horizontal" >
<LinearLayout 
    android:layout_alignParentLeft="true"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
    android:id="@+id/action_bar_button_about"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"           
    android:layout_weight="1"
    android:background="@drawable/ios_btn"
    android:text="about" />

    <Button
    android:id="@+id/action_bar_button_reload"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/ios_btn"          
    android:layout_weight="1"
    android:text="reload" />
</LinearLayout>    
</RelativeLayout>

MainActivity.java

package jb.cydia;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   
    final ActionBar ab = getActionBar();
    ab.setDisplayShowHomeEnabled(false);
    ab.setDisplayShowTitleEnabled(false);     
    final LayoutInflater inflater =    (LayoutInflater)getSystemService("layout_inflater");
    View view = inflater.inflate(R.layout.activity_main_ab,null); 
    ab.setCustomView(view);
    ab.setDisplayShowCustomEnabled(true);

}
}

哦,差点忘了它是 android,即使它看起来像是来自 ios 的屏幕截图 .. 我把我的手机植根后看起来像那样 :) androids 关于定制对吗?

【问题讨论】:

    标签: android android-actionbar android-xml android-button


    【解决方案1】:
    <Button
        android:layout_width="fill_parent"
    ... />
    

    应该是

    <Button
        android:layout_width="wrap_content"
    ... />
    

    【讨论】:

    • 如果你对按钮的高度属性(设置layout_height="wrap_content")执行相同的操作。
    • 如何让按钮变小,就像在 IOS 操作栏中一样,按钮比操作栏小,但我的按钮和操作栏没有空间?
    • 阅读有关在按钮上设置边距的信息 - this 答案是一个不错的起点,但听起来您确实需要阅读 Android Layout documentation。该页面涵盖了您在这里询问的所有内容以及更多内容。
    【解决方案2】:

    您应该始终为按钮使用 9 个补丁图像。检查this 站点以创建您的 9 个补丁。并查看this 视频。

    【讨论】: