마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

지정한 위치에버튼이 있지않습니다.

0 추천

문제의화면

각 줄의 y값이랑 버튼의 y값이랑 일치시키고싶었습니다.

점점 버튼이 추가되면서 버튼이 줄위로 줄의y값보다 작게, 지정되고있습니다.

(위에서 두번째 라인은 왼쪽에서 두번째버튼이랑 같은 높이에, 이런식으로.....)

밑에는 이에대한 소스입니다.

어느부분이 문제일지 알려주시면 안될까요?

 

xml -

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/rootView"
            >

            <LinearLayout
                android:id="@+id/frontView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:orientation="horizontal"></LinearLayout>

            <LinearLayout
                android:id="@+id/backView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:orientation="vertical"></LinearLayout>
        </RelativeLayout>

</android.support.constraint.ConstraintLayout>

java-

package com.dyco.testapp;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    LinearLayout frontLayout,backLayout;
    RelativeLayout rootLayout;
    ArrayList<Integer> linesYs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rootLayout = findViewById(R.id.rootView);
        frontLayout = findViewById(R.id.frontView);
        backLayout = findViewById(R.id.backView);
        frontLayout.setX(rootLayout.getX());
        frontLayout.setY(rootLayout.getY());
        backLayout.setX(frontLayout.getX());
        backLayout.setY(frontLayout.getY());

        linesYs = new ArrayList<>();
        backLayout.post(new Runnable() {
            @Override
            public void run() {
                LinearLayout.LayoutParams lineParm = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2);
                int lineSeed = Math.round(frontLayout.getHeight() / 10);
                for (int i = 0; i < 10; i++) {
                    LinearLayout line = new LinearLayout(backLayout.getContext());
                    line.setBackgroundColor(Color.BLACK);
                    backLayout.addView(line, lineParm);
                    line.setY(lineSeed * i);
                    linesYs.add((int) line.getY());
                }

                for (int i:linesYs) {
                    Button btn = new Button(frontLayout.getContext());
                    btn.setText("tt");
                    btn.setBackgroundColor(Color.BLUE);
                    frontLayout.addView(btn);
                    btn.setY(i);
                }
            }
        });
    }
}

 

익명사용자 님이 2018년 10월 5일 질문
for (int i:linesYs)

-->
for (float i:linesYs)

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...