简体   繁体   中英

Why is the text not appearing on this Button?

For some reason, when creating a LinearLayout with buttons, the leftmost buttons do not have any text.

In the layout preview, it looks fine. But on the actual device, it looks like this: 布局破碎

Here is the layout code:

<Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/minusOneButton"
        android:layout_gravity="center_horizontal"
        tools:text="-"
        android:textSize="25sp"
        android:textAlignment="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/minus5Button"
        android:layout_gravity="center_horizontal"
        tools:text="-5"
        android:textSize="25sp"
        android:textAlignment="center"
        android:layout_below="@+id/minusOneButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="+"
        android:id="@+id/plusOneButton"
        android:layout_gravity="center_horizontal"
        android:textSize="25sp"
        android:textAlignment="center"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/counterText" />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="+5"
        android:id="@+id/plus5Button"
        android:layout_gravity="center_horizontal"
        android:textSize="23sp"
        android:textAlignment="center"
        android:layout_below="@+id/plusOneButton"
        android:layout_alignLeft="@+id/plusOneButton"
        android:layout_alignStart="@+id/plusOneButton" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="0"
        android:id="@+id/counterText"
        android:layout_gravity="center_horizontal"
        android:textSize="60dp"
        android:layout_marginBottom="13dp"
        android:layout_alignBottom="@+id/plus5Button"
        android:layout_toRightOf="@+id/minusOneButton"
        android:layout_toEndOf="@+id/minusOneButton" />

Even when having only one button in this layout, it does no show the text.

Here is the class that is used to make the class usable by other layouts:

public class HigherCounter extends LinearLayout implements View.OnClickListener{

    public HigherCounter(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = LayoutInflater.from(context);

        inflater.inflate(R.layout.higher_counter, this);
}

Thanks!

You are using tools:text in your first 2 buttons, and that attribute is used only to renderer the preview screen.
Replace them by android:text and they will appear at runtime.

You are using the tools: namespace. this only shows up in the preview tool.

Change this to android: if it is just static text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM