2016-04-16 11 views
0

Ich habe eine Klasse, dieWie replcae AbsoluteLayout mit RelativeLayout

this.guageBack= (AbsoluteLayout) findViewById(R.id.gaugeFrame); 
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY); 
gaugeNeedle.setLayoutParams(params); 

AbsoluteLayout unten Code darin verwendet veraltet so, wenn ich will RelativeLayout verwenden, die ich nicht für

AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY); 

Replcement haben, weil es akzeptiert zwei Argumente wie unten

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight); 

Was soll ich tun?

+0

Überprüfung dieser http://stackoverflow.com/questions/8477164/position-view-in-relative-out-Laufzeit und lesen Sie die relative Layout-Dokumentation. Es ist gemeint, wenn Sie eine Ansicht relativ zu einer anderen platzieren möchten – Raghunandan

Antwort

1

einfach diesen Code hinzufügen, anstatt von Ihnen

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight); 
     params.topMargin = needleY; 
     params.leftMargin = needleX; 
0

Machen Sie es wie dieser

public class CodeLayout extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Creating a new RelativeLayout 
     RelativeLayout relativeLayout = new RelativeLayout(this); 

     // Defining the RelativeLayout layout parameters. 
     // In this case I want to fill its parent 
     RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.FILL_PARENT, 
       RelativeLayout.LayoutParams.FILL_PARENT); 

     // Creating a new TextView 
     TextView tv = new TextView(this); 
     tv.setText("Test"); 

     // Defining the layout parameters of the TextView 
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
     lp.addRule(RelativeLayout.CENTER_IN_PARENT); 

     // Setting the parameters on the TextView 
     tv.setLayoutParams(lp); 

     // Adding the TextView to the RelativeLayout as a child 
     relativeLayout.addView(tv); 

     // Setting the RelativeLayout as our content view 
     setContentView(relativeLayout, rlp); 
    } 
} 
1

diesen Code versuchen,

AbsoluteLayout absoluteLayout = //get absolute layout 

Button button = new Button(); 
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams(); 
params.x = 100; 
params.y = 100; 

absoluteLayout.addView(button, params);