RelativeLayout.LayoutParams和addRule方法不起作用

时间:2015-07-02 16:06:42

标签: android

我是新来的,所以请对我温柔:)​​。

我试图在每个按钮点击时生成一个新的TextView,并将它放在活动中的另一个视图下面。

我的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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mylifeinformationsharedsocial.HairCutsActivity"
    android:id="@+id/relative_layout_id">
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:id="@+id/header1"
        android:text="@string/header1_text"
        android:textSize="20sp"
        
        />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:id="@+id/editText1"
        android:layout_below="@id/header1"
        android:text="@string/date_text"
        android:inputType="date"
         />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addButton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="@string/add_button_text"
        
        />
    


</RelativeLayout>

我的Java:

package com.example.mylifeinformationsharedsocial;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.example.mylifeinformationsharedsocial.R.id;

import android.support.v7.app.ActionBarActivity;
import android.text.Layout;
import android.R.bool;
import android.R.integer;
import android.R.layout;
import android.R.string;
import android.app.ActionBar.LayoutParams;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HairCutsActivity extends ActionBarActivity 
{
    TextView header1;
    EditText editText; // Where you insert your new dates
    Button addButton;
    String hairCutDateString;
    TextView dateText;
    public static ArrayList<TextView> datesUploadedList = new ArrayList<TextView>();
    int datesUploadedCounter = 0;
    RelativeLayout relativeLayout;
    boolean firstInsertFlag = true; // True - first insert


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hair_cuts);

        getWindow().getDecorView().setBackgroundColor(Color.WHITE); // Setting the window color to white

        header1 = (TextView) findViewById(R.id.header1);
        header1.setTextColor(Color.rgb(32,178,170));

        editText = (EditText) findViewById(R.id.editText1);
        relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout_id);
        addButton = (Button) findViewById(R.id.addButton);

        addButton.setOnClickListener(new View.OnClickListener() // Click on add button
        {
            @Override
            public void onClick(View v)
            {
                SimpleDateFormat simpleDateFromat = new SimpleDateFormat("MM/dd/yyyy");
                java.util.Date date;
                try
                {
                    date = simpleDateFromat.parse(editText.getText().toString());
                    hairCutDateString = date.getMonth() + 1 + "/" + (date.getDate()) + "/" + (1900 + date.getYear()); // The date string
                }
                catch (ParseException e)
                {
                    e.printStackTrace();
                }
                dateText = new TextView(HairCutsActivity.this);
                dateText.setText(hairCutDateString);
                dateText.setId(datesUploadedCounter);
                datesUploadedCounter++;

                datesUploadedList.add(dateText);
                if (firstInsertFlag) // First insert
                {
                    RelativeLayout.LayoutParams layoutParams  = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                    // Setting the new date below the header
                    layoutParams .addRule(RelativeLayout.BELOW, header1.getId()); 
                    dateText.setLayoutParams(layoutParams);
                    relativeLayout.addView(dateText);
                    layoutParams.addRule(RelativeLayout.BELOW,datesUploadedList.get(datesUploadedCounter - 1).getId());
                    editText.setLayoutParams(layoutParams);
                    firstInsertFlag = false;
                }
                else 
                {

                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.hair_cuts, 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();
        if (id == R.id.action_settings) 
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

现在问题是EditText的BELOW对齐工作(低于新的TextView)但是新TextView的对齐方式低于header1 TextView无法正常工作。

图片:

点击按钮之前:

http://www.siz.co.il/my.php?i=fmigy2wimnkz.png

点击按钮后:

http://www.siz.co.il/my.php?i=ijyg0ytz4nty.png

任何人都知道这是什么问题?感谢

1 个答案:

答案 0 :(得分:0)

只需更改

 layoutParams.addRule(RelativeLayout.BELOW,datesUploadedList.get(datesUploadedCounter - 1).getId());

 layoutParams.addRule(RelativeLayout.BELOW,datesUploadedList.get(dateText.getId());

此外,您可能需要设置一些余量。

你不需要处理第一次添加和其他人如此不同。

采用全局TextView

 TextView lastTextView = null;

只需使用(根据您的口味改变边距)

    onclick(){
    .........

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            if(lastTextView == null) {
                layoutParams.addRule(RelativeLayout.BELOW, header1.getId());
            }else{
                layoutParams.addRule(RelativeLayout.BELOW,lastTextView.getId());
            }
           layoutParams.setMargins(100,100,100,100);
            dateText.setLayoutParams(layoutParams);
            relativeLayout.addView(dateText);

            RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            newLayoutParams.addRule(RelativeLayout.BELOW, dateText.getId());
            newLayoutParams.setMargins(200,200,200,200);
            editText.setLayoutParams(newLayoutParams);
            lastTextView =dateText;
    }