태그 보관물: position

position

뷰의 절대 위치 설정 않습니다 …) 예를 들어, 240x320px 화면이있는

Android에서 뷰의 절대 위치를 설정할 수 있습니까? (가 있다는 것을 알고 AbsoluteLayout있지만 더 이상 사용되지 않습니다 …)

예를 들어, 240x320px 화면이있는 경우 ImageView가운데가 (100,100) 위치에 있도록 20x20px 인 화면을 어떻게 추가 할 수 있습니까?



답변

RelativeLayout을 사용할 수 있습니다. 레이아웃 내부의 위치 (50,60)에 30×40 ImageView를 원한다고 가정 해 봅시다. 당신의 활동 어딘가에 :

// Some existing RelativeLayout from your layout xml
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);

ImageView iv = new ImageView(this);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);

더 많은 예 :

(50,60) 및 (80,90)에 각각 2 개의 30×40 ImageView (노란색 1 개, 빨간색 1 개)를 배치합니다.

RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv;
RelativeLayout.LayoutParams params;

iv = new ImageView(this);
iv.setBackgroundColor(Color.YELLOW);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);

iv = new ImageView(this);
iv.setBackgroundColor(Color.RED);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 80;
params.topMargin = 90;
rl.addView(iv, params);

하나의 30×40 노랑 ImageView를 (50,60)에 배치하고 다른 30×40 빨강 ImageView <80,90> 노랑 ImageView에 상대적으로 배치 합니다.

RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv;
RelativeLayout.LayoutParams params;

int yellow_iv_id = 123; // Some arbitrary ID value.

iv = new ImageView(this);
iv.setId(yellow_iv_id);
iv.setBackgroundColor(Color.YELLOW);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);

iv = new ImageView(this);
iv.setBackgroundColor(Color.RED);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 80;
params.topMargin = 90;

// This line defines how params.leftMargin and params.topMargin are interpreted.
// In this case, "<80,90>" means <80,90> to the right of the yellow ImageView.
params.addRule(RelativeLayout.RIGHT_OF, yellow_iv_id);

rl.addView(iv, params);


답변

일반적으로 leftMargin 및 topMargin 속성을 지정하여 FrameLayout을 컨테이너로 사용하여 특정 위치에보기를 추가 할 수 있습니다 .

다음 예제는 FrameLayout을 전체 화면 컨테이너로 사용하여 위치 (100,200)에 20x20px ImageView를 배치합니다.

XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:background="#33AAFF"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

활동 / 조각 / 사용자 정의보기

//...
FrameLayout root = (FrameLayout)findViewById(R.id.root);
ImageView img = new ImageView(this);
img.setBackgroundColor(Color.RED);
//..load something inside the ImageView, we just set the background color

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(20, 20);
params.leftMargin = 100;
params.topMargin  = 200;
root.addView(img, params);
//...

여백을 RelativeLayout없이 절대 (X, Y) 좌표로 사용할 수 있기 때문에 트릭을 수행합니다.

여기에 이미지 설명을 입력하십시오


답변

위의 Andy Zhang의 대답에 추가하기 만하면 원하는 경우 rl.addView에 매개 변수를 제공 한 다음 나중에 변경할 수 있습니다.

params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);

다음과 같이 잘 쓸 수 있습니다.

params = new RelativeLayout.LayoutParams(30, 40);
rl.addView(iv, params);
params.leftMargin = 50;
params.topMargin = 60;

따라서 params 변수를 유지하면 iv를 rl에 추가 한 후 언제든지 iv의 레이아웃을 변경할 수 있습니다.


답변

코드의 픽셀 값을 하드 코딩하지 않고도보다 깨끗하고 역동적 인 방법입니다.

클릭 한 버튼 바로 아래에 대화 상자 (즉시 팽창)를 배치하고 싶었습니다.

이 방법으로 해결 :

    // get the yoffset of the position where your View has to be placed 
    final int yoffset = < calculate the position of the view >

    // position using top margin
    if(myView.getLayoutParams() instanceof MarginLayoutParams) {
        ((MarginLayoutParams) myView.getLayoutParams()).topMargin = yOffset;
    }

그러나의 부모 레이아웃 myView이의 인스턴스 인지 확인해야합니다 RelativeLayout.

더 완전한 코드 :

    // identify the button
    final Button clickedButton = <... code to find the button here ...>

    // inflate the dialog - the following style preserves xml layout params
    final View floatingDialog =
        this.getLayoutInflater().inflate(R.layout.floating_dialog,
            this.floatingDialogContainer, false);

    this.floatingDialogContainer.addView(floatingDialog);

    // get the buttons position
    final int[] buttonPos = new int[2];
    clickedButton.getLocationOnScreen(buttonPos);
    final int yOffset =  buttonPos[1] + clickedButton.getHeight();

    // position using top margin
    if(floatingDialog.getLayoutParams() instanceof MarginLayoutParams) {
        ((MarginLayoutParams) floatingDialog.getLayoutParams()).topMargin = yOffset;
    }

이렇게하면 Java 코드에서 해당 픽셀 / dp를 하드 코딩하는 대신 대상 XML보기가 레이아웃 XML 파일을 사용하여 설정된 레이아웃 매개 변수에 맞게 조정될 수 있습니다.


답변

스크린 샷 확인

원하는 X & Y 지점 에 대한 견해를 두십시오

레이아웃 파일

<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"
    tools:context="com.example.test.MainActivity" >

    <AbsoluteLayout
        android:id="@+id/absolute"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/rlParent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/img"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/btn_blue_matte" />
        </RelativeLayout>
    </AbsoluteLayout>

</RelativeLayout>

자바 클래스

public class MainActivity extends Activity {

    private RelativeLayout rlParent;
    private int width = 100, height = 150, x = 20, y= 50;

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

        AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(width, height, x, y);
        rlParent = (RelativeLayout)findViewById(R.id.rlParent);
        rlParent.setLayoutParams(param);
    }
}

끝난


답변

누군가를 도울 수있는 경우를 대비 하여이 애니메이터 ViewPropertyAnimator 를 다음과 같이 사용해보십시오.

myView.animate().x(50f).y(100f);

myView.animate().translateX(pixelInScreen) 

참고 :이 픽셀은 뷰와 관련이 없습니다. 이 픽셀은 화면의 픽셀 위치입니다.

bpr10 답변에 크레딧


답변

특정 위치에서보기를 설정하려면 아래 코드를 시도하십시오.

            TextView textView = new TextView(getActivity());
            textView.setId(R.id.overflowCount);
            textView.setText(count + "");
            textView.setGravity(Gravity.CENTER);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            textView.setTextColor(getActivity().getResources().getColor(R.color.white));
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // to handle click 
                }
            });
            // set background 
            textView.setBackgroundResource(R.drawable.overflow_menu_badge_bg);

            // set apear

            textView.animate()
                    .scaleXBy(.15f)
                    .scaleYBy(.15f)
                    .setDuration(700)
                    .alpha(1)
                    .setInterpolator(new BounceInterpolator()).start();
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = 100; // margin in pixels, not dps
            layoutParams.leftMargin = 100; // margin in pixels, not dps
            textView.setLayoutParams(layoutParams);

            // add into my parent view
            mainFrameLaout.addView(textView);