Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Android Java: ein View-Element als Float erstellen

25.09.2018 (👁218)

Android Java: ein View-Element als Float erstellen

Aufgabe: Es soll zur Laufzeit beim Klick auf einen Button ein Textfeld über der aktuellen Position des Buttons erscheinen.

Lösung:

Man muss die Position des Buttons bestimmen mit getLeft();

Dann ein Relatives Layout erstellen und die Position zuweisen.

Dann das Relative-Layout einem neuen View-Element zuweisen.

(Button oder Text-Eingabe)

public void btnTest_onClick(View view) {
   
//------------< btnTest_onClick() >-----------
    //< srcButton >
   
Integer intLeft=view.getLeft();
    Integer intTop=view.getTop();
    Integer intWidth=view.getWidth();
    Integer intHeight=view.getHeight();
   
//</ srcButton >

    //< set_Position >
   
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
   
//params.setMargins(100, 200, 0,0);
   
params.setMargins(intLeft+intWidth, intTop-150, 0,0);
    params.addRule(RelativeLayout.
ALIGN_LEFT);
    params.addRule(RelativeLayout.
ALIGN_TOP);
   
//</ set_Position >

    //< Float_Element >
    //Button btnFloat=new Button(this);
   
EditText ctlFloat=new EditText(this);
    String sPosition= String.valueOf(intLeft);
    ctlFloat.setText(
"Button Left=" + sPosition);
    ctlFloat.setBackgroundColor(Color.
YELLOW);
    ctlFloat.setLayoutParams(params);
   
//</ Float_Element >

    //< Show_Element >
   
RelativeLayout root_Panel= findViewById(R.id.base_Layout);
    root_Panel.addView(ctlFloat);
   
//</ Show_Element >
    //------------</ btnTest_onClick() >-----------
}

 

 

 

 

Activity_main.xml

Standard Seite in Android App

Wichtig: hier ist nur das Basis-Layout als Relatives Layout. Das bedeutet, dass  alle Element nach Abständen Top, Left in der Position bestimmt werden.

 

Activity_main.xml

Unter res/layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context="com.codedocu.test03_floatedit.MainActivity">

    <
RelativeLayout
       
android:id="@+id/base_Layout"
       
android:layout_height="match_parent"
       
android:layout_width="match_parent"
       
android:background="#eeeeee"
       
>

        <
Button
           
android:id="@+id/btnTest"
           
android:layout_width="200px"
            
android:layout_height="200px"
           
android:layout_alignParentStart="true"
           
android:layout_alignParentTop="true"
           
android:layout_marginStart="43dp"
           
android:layout_marginTop="118dp"
           
android:text="Test"
            
android:onClick="btnTest_onClick"
           
/>
    </
RelativeLayout>
</
android.support.constraint.ConstraintLayout>

 

 

 

 

 

Completter java code:

package com.codedocu.test03_floatedit;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

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

   
public void btnTest_onClick(View view) {
       
//------------< btnTest_onClick() >-----------
        //< srcButton >
       
Integer intLeft=view.getLeft();
        Integer intTop=view.getTop();
        Integer intWidth=view.getWidth();
        Integer intHeight=view.getHeight();
       
//</ srcButton >

        //< set_Position >
       
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        
//params.setMargins(100, 200, 0,0);
       
params.setMargins(intLeft+intWidth, intTop-150, 0,0);
        params.addRule(RelativeLayout.
ALIGN_LEFT);
        params.addRule(RelativeLayout.
ALIGN_TOP);
       
//</ set_Position >

        //< Float_Element >
        //Button btnFloat=new Button(this);
       
EditText ctlFloat=new EditText(this);
        String sPosition= String.valueOf(intLeft);
        ctlFloat.setText(
"Button Left=" + sPosition + " Overlay");
        ctlFloat.setBackgroundColor(Color.
YELLOW);
        ctlFloat.setLayoutParams(params);
       
//</ Float_Element >

        //< Show_Element >
       
RelativeLayout root_Panel= findViewById(R.id.base_Layout);
        root_Panel.addView(ctlFloat);
       
//</ Show_Element >
        //------------</ btnTest_onClick() >-----------
   
}

}



//*wrong nomatch
//btnFloat.setLeft(500);
//btnFloat.setTop(500);
//btnFloat.setPadding(300,300,0,0);
//btnFloat.layout(300,300,220,220);
//btnFloat.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
//btnFloat.setGravity(Gravity.LEFT);

//String sPosition= String.valueOf(btnFloat.getLeft());
//String sPosition= String.valueOf(btnFloat.getLayoutParams().width);

//Integer intLeft=view.getPaddingLeft();
//Integer intTop=view.getPaddingTop();