如何相对于设备屏幕大小放置图像按钮

时间:2013-11-20 15:32:53

标签: java android xml

我有一张图片,按钮的所有文字已经就在它上面,我想在每个单词的顶部放一个透明按钮。 我不能使用XML代码,因为如果我希望按钮留在文本上,代码必须改变。

以下是XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/backgroundMenue"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="Nothing"
        android:scaleType="centerCrop"
        android:src="@drawable/main_menu2" />

    <ImageButton
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="92dp"
        android:layout_marginRight="218dp" />

</RelativeLayout>

Java代码:

package com.example.snakesmash;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

    ImageButton play;
    ImageView background;
    RelativeLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        layout = (RelativeLayout) RelativeLayout.inflate(this, R.layout.activity_main, null);

        play = (ImageButton) layout.findViewById(R.id.play);
        background = (ImageView) layout.findViewById(R.id.backgroundMenue);

        play.layout((int) Math.round(background.getHeight() * 0.65), (int) Math.round(background.getWidth() * 0.65), (int) Math.round(background.getWidth() * 0.35), (int) Math.round(background.getHeight() * 0.35)); 

        setContentView(layout);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

0 个答案:

没有答案