如何防止相对布局中的重叠?

时间:2015-05-16 09:11:31

标签: android android-layout gridview android-relativelayout

我有一个活动在顶部显示textview,在其下面显示gridview,我以编程方式添加所有元素(相对布局,textview,gridview)。 我的问题是重叠,元素显示在彼此之上。 它显示如下: enter image description here 即使我使用addRule(),但它仍然没有按我的意愿显示它们。 任何人都可以告诉我这段代码中我错过了哪些内容?

GameActivity代码:

package spellwords.com.example.hp.spellwords;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Random;


public class GameActivity extends ActionBarActivity {


    public static Context mContext;//save the context
    public static List<Word> allWords;//save the word-type returned from the WordsDAO
    public static String randomStr;//Save the random word that has been chosen to be shown for the user
    public static String[] stringArray;//To save the word converted vector into string array
    public static TextView mSelectedWordPlace;

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

        //Get the application context
        mContext = getApplicationContext();

       //Create an object to process operation on words table
        WordsDAO wDAO = new WordsDAO(mContext);

       //Get all the words in table words
        allWords = wDAO.getAllWords();

       //Create new String array to save the allWords (victor object value by value)
        stringArray = new String[allWords.size()];
        for (int i=0; i < allWords.size(); i++) {
            stringArray[i] = allWords.get(i).toString();
        }

        //Get the Word to be shown to the player randomly from the stringArray
        randomStr = stringArray[new Random().nextInt(stringArray.length)];


        // create a RelativeLayout
        RelativeLayout relativeLayout = new RelativeLayout(this);

        // define the RelativeLayout layout parameters.
        RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

        mSelectedWordPlace = new TextView(mContext);
        mSelectedWordPlace.setTextSize(40);
        mSelectedWordPlace.setTextColor(Color.parseColor("#000000"));
        mSelectedWordPlace.setText(randomStr);
        relativeLayout.addView(mSelectedWordPlace, relativeLayoutParams);

        // create a gridview
        GridView gridView= new GridView(this);
        gridView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT));
        gridView.setNumColumns(5);
        gridView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
        gridView.setAdapter(new ImageAdapter(this));

        // Create a message handling object as an anonymous class.
         AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                v.setBackgroundColor(0xFF00FF00);

            }
        };

        gridView.setOnItemClickListener(mMessageClickedHandler);
        //gridView.setAdapter(new ImageAdapter(this));

        // Adding the gridview to the RelativeLayout as a child
        relativeLayout.addView(gridView);

        relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        // set the RelativeLayout as our content view
        setContentView(relativeLayout, relativeLayoutParams);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案