0

我通过玩组件的边距为自己做了一个幻灯片动画。

不幸的是,当我将正确的元素(编辑文本)“推”出屏幕时,它会调整它的大小而不是切割边缘部分。

我可以通过影响例如 1000dp 到 editText 来让它表现得像我想要的那样,但我希望这个 editText 适合屏幕的宽度,而不是硬编码的宽度。

开始画面:

开始画面

错误行为:

错误的行为

良好的行为:

良好的行为

这是我的自定义根组件中“动画”的代码:

package com.example.components;

import com.example.database.NoteDataSource;
import com.example.memonotepad.R;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
import android.app.Activity;

public class SlideRightNLeft extends LinearLayout{

private LinearLayout.LayoutParams params1;

int x;

private SlideRightNLeft srnl;

private Context context_f;

private int slideDistance;


public SlideRightNLeft(Context context) {
    super(context);
}

public SlideRightNLeft(Context context, AttributeSet attrs) {
    super(context, attrs);
    context_f = context;
    srnl = this;
    slideDistance = (int) context_f.getResources().getDimension(R.dimen.slide_distance);

}

public SlideRightNLeft(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}



public void slideLogin(){


    Thread slideIt = new Thread(new Runnable() {
        @Override
        public void run() {

            x = 0;
            params1 = new LinearLayout.LayoutParams(srnl.getChildAt(0).getWidth(), srnl.getChildAt(0).getHeight());

            while(x < slideDistance) {

                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                ((Activity)context_f).runOnUiThread(new Runnable(){

                    @Override
                    public void run() {

                        int diff = x - slideDistance;

                        params1.setMargins(diff, 1, 1, 1);

                        srnl.getChildAt(0).setLayoutParams(params1);

                        srnl.invalidate();

                    }
                });

                x++;

            }
        }
    });

    slideIt.start();

}




}

活动代码:

package com.example.memonotepad;

import java.util.Calendar;
import java.util.Date;

import com.example.components.SlideRightNLeft;
import com.example.database.NoteDataSource;
import com.example.adapter.NotesAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MemoNotepad extends Activity {

private SlideRightNLeft mainGroupComponent;
private Button slideLogin;
private Button addNote;

private NoteDataSource nds;

private Date rightNowDate;

private ListView lvNote;

private Cursor lvCursor;

private NotesAdapter na;

private EditText edt;

public static int width;

WindowManager mWinMgr;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memo_notepad);

    mWinMgr = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
    width = mWinMgr.getDefaultDisplay().getWidth();

    edt = (EditText) findViewById(R.id.textBook);

    edt.setWidth(width);
    edt.setMinimumWidth(width);

    mainGroupComponent = (SlideRightNLeft) findViewById(R.id.mainGroupLayout);
    nds = new NoteDataSource(this);

    lvNote = (ListView) findViewById(R.id.listNotes);

    lvCursor = nds.getAllNotes();

    na = new NotesAdapter(this, lvCursor);
    lvNote.setAdapter(na);

    slideLogin = (Button) findViewById(R.id.login_button);
    slideLogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mainGroupComponent.slideLogin();
        }
    });

    addNote = (Button) findViewById(R.id.new_note);
    addNote.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Calendar rightNow = Calendar.getInstance();
            rightNowDate = rightNow.getTime();  // dt is now the new date
            nds.createNote("Note", "text inside", rightNowDate);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_memo_notepad, menu);
    return true;
}
}
4

1 回答 1

1

我可以通过影响例如 1000dp 到 editText 来让它表现得像我想要的那样,但我希望这个 editText 适合屏幕的宽度,而不是硬编码的宽度。

为什么不获取设备的宽度?

mWinMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int displayWidth = mWinMgr.getDefaultDisplay().getWidth();
于 2013-01-19T07:44:27.533 回答