【问题标题】:Android button text out of borderAndroid按钮文本超出边界
【发布时间】:2015-05-03 18:26:37
【问题描述】:

在 google 中查找并在 from 中设置边界以使 RelativeLayout 中的文本适合按钮大小。

我的应用中的示例: http://tinypic.com/view.php?pic=2wec9d5&s=8#.VUZns_lVhBc

我的 xml 按钮:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_game"
    tools:context=".ClassicMode" >
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:scaleType="fitXY"
        android:scaleX="2"
        android:scaleY="2" />

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal" >
    </LinearLayout>

    <TextView
        android:id="@+id/numOfGuesses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="23dp"
        android:layout_toLeftOf="@+id/linearLayout1"
        android:layout_toRightOf="@+id/gallery1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/guesses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/guesses"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="60px" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="23dp"
        android:layout_marginTop="18dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="226dp"
            android:layout_height="152dp"
            android:scaleX="1"
            android:scaleY="1"
            android:src="@drawable/argentina" />
    </LinearLayout>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:background="@android:color/transparent"
        android:onClick="check"
        android:scaleX="1.5"
        android:scaleY="1.5"
        android:text="Button"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:background="@android:color/transparent"
        android:onClick="check"
        android:scaleX="1.5"
        android:scaleY="1.5"
        android:text="Button"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_below="@+id/button3"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:background="@android:color/transparent"
        android:onClick="check"
        android:scaleX="1.5"
        android:scaleY="1.5"
        android:text="Button"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:background="@android:color/transparent"
        android:onClick="check"
        android:scaleX="1.5"
        android:scaleY="1.5"
        android:text="Button"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/scorenum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/linearLayout1"
        android:layout_alignParentLeft="true"
        android:textSize="60px" />

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/scorenum"
        android:layout_alignParentLeft="true"
        android:text="@string/score"
        android:textSize="60px" />

    <Button
        android:id="@+id/hint"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/circle"
        android:gravity="center_vertical|center_horizontal"
        android:text="hint"
        android:textColor="#fff" />

</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-button


    【解决方案1】:

    尝试在按钮上设置以下属性:

    android:layout_marginRight="0dp"
    android:layout_marginLeft="0dp"
    android:singleLine="false" // set this to true if you want your text to be single line
                               // or to false otherwise
    

    我建议从你的按钮中删除这一行:

    android:layout_alignLeft="some_button_id"
    

    【讨论】:

    • 仍然超出边界,但稍微好一点,我认为只有一个字会出现
    • @Adi 您能否在此处粘贴另一个屏幕截图并指出哪些按钮超出范围?
    • @Adi 实际上,当我将您的代码粘贴到我的编辑器中时 - 它看起来不错。无需调整。你确定你没有从代码中改变你的按钮样式吗?
    • 我改变了你告诉我改变或添加为 layoutMarginiLeft/Right 我没有触及我的意思的风格
    猜你喜欢
    • 2020-03-27
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多