如何将一个按钮覆盖在另一个按钮上

时间:2017-06-28 06:53:04

标签: android android-layout

我希望在两行中有0到9个按钮。每个按钮都在右下角有另一个小按钮的叠加。当我点击按钮时,计数将显示在覆盖按钮上。感谢我的英语不好提前感谢

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:orientation="horizontal">
   <LinearLayout
    android:id="@+id/expandable2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
      RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/btn0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0" />

            <Button
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_alignBottom="@id/btn0"
                android:layout_alignRight="@id/btn0"
                android:background="#FFFFFF"
                android:text="0" />
        </RelativeLayout>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9" />
    </LinearLayout>
    </LinearLayout>
    </LinearLayout>

我的输出 https://program.dotfit.com/startprogram.aspx

任何其他建议将不胜感激

3 个答案:

答案 0 :(得分:0)

尝试像这样设置

 android:layout_marginLeft="-20dp"
 android:layout_marginTop="10dp"

答案 1 :(得分:0)

使用framelayout

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">

        <Button
            android:id="@+id/icon_image"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@color/black"
            android:text="0" />

        <Button                
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="bottom|right"
            android:background="@color/white"
            android:layout_marginLeft="6dp"
            android:src="@drawable/ic_goal_check" />
    </FrameLayout>

Something like this

之后,您可以为所有按钮重复使用此布局。要在xml中重复使用它,请按如下方式创建自定义视图:

public class YourButton extends FrameLayout {

    private Button buttonOne, buttonTwo;

    public YourButton(Context context) {
        super(context);
        this.context = context;
        initializeViews();
    }

    public YourButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GoalButton);
        typedArray.recycle();
        initializeViews();
    }

    private void initializeViews() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.your_button_layout, this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        buttonOne = (TextView) this.findViewById(R.id.button_one);
        buttonTwo = (ImageView) this.findViewById(R.id.button_two);
    }

    public void setCount(String count){
        buttonTwo.setText(count);
    }
}

现在您可以在布局中使用

<yourPackageName.YourButton
      android:id="@+id/button_one"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

这将使您的主要布局简洁。

答案 2 :(得分:0)

使用textview显示计数,因为按钮上方的按钮无意义。 无论如何如何展示它。

countBtn

在Grid或RecyclerView适配器中使用此视图,使其显示10次。

如果你想使用TextView显示Count删除import boto3 elbList = boto3.client('elb') bals = elbList.describe_load_balancers() for elb in bals['LoadBalancerDescriptions']: print 'ELB Name:' + elb['LoadBalancerName'] + 'ELB scheme type: ' + elb['Scheme'] 并使其成为一个textView,其最后两个属性相同。