2016-05-15 107 views
0

我想沒有XML是否有可能在Android中創建沒有xml的視圖?

public class MyView extends ViewGroup { 

    public MyView (Context context) { 
     super(context); 
     setBackgroundColor(0xffff0000); 

     RelativeLayout base = new RelativeLayout(context); 

     RelativeLayout.LayoutParams rP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     base.setBackgroundColor(0xff00ff00); 
     base.setLayoutParams(rP); 

     RelativeLayout.LayoutParams iP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     ImageView icon = new ImageView(context); 
     icon.setBackgroundResource(android.R.drawable.ic_menu_add); 
     icon.setLayoutParams(iP); 

     addView(icon); 
     addView(base); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 

    } 
} 

創建自定義視圖和actitvy

MyView myview = new MyView(this); 
WindowManager.LayoutParams baseParams = new WindowManager.LayoutParams(
     WindowManager.LayoutParams.MATCH_PARENT, 
     WindowManager.LayoutParams.MATCH_PARENT, 
     WindowManager.LayoutParams.TYPE_PHONE, 
     WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
     PixelFormat.TRANSLUCENT); 
addContentView(myview, baseParams); 

使用它,但的LayoutParams不起作用,它什麼都不顯示

我必須使用此onLayout以使其顯示,並且基本佈局不是MATCH_PARENT而圖標不是WRAP_CONTENT

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    final View child = getChildAt(0); 
    final View child2 = getChildAt(1); 

    child.layout(0, 0, 300, 300); 
    child2.layout(0, 0, 300, 300); 
} 

我也試圖圖標添加到佈局,但應用程序會崩潰

base.addView(icon); 

是否有可能不硬編碼來創建此佈局的大小和位置?

我檢查了RelativeLayout.LayoutParams,但我找不到任何方法將它設置爲centerInParent爲true。

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@android:drawable/ic_menu_add" 
     android:layout_centerInParent="true" 
     /> 
</RelativeLayout> 
+2

'WindowManager.LayoutParams'不適用於'Activity'中的'View's。我不確定你想要做什麼,但是如果你希望你的自定義'View'作爲整個佈局,只需調用'setContentView()'。否則,將其添加到'Activity'佈局中的'ViewGroup'。爲了以'RelativeLayout.LayoutParams'爲中心,你正在尋找'addRule()'方法。 –

+0

另外,'ViewGroup'不知道佈置其子項。要麼實際實現自定義'ViewGroup'的其餘代碼(例如填充空的'onLayout()'方法),要麼擴展其他具有特定佈局規則的類(例如,擴展'RelativeLayout'而不是試圖包裝'RelativeLayout')。 – CommonsWare

回答

0

請給這個代碼一試。

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 

public class MainActivity extends AppCompatActivity { 

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

     // Create parent RelativeLayout 
     RelativeLayout relParent = new RelativeLayout(this); 
     RelativeLayout.LayoutParams relParentParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 
     relParent.setLayoutParams(relParentParam); 

     // Create child ImageView 
     ImageView imgView = new ImageView(this); 
     RelativeLayout.LayoutParams imgViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     imgView.setLayoutParams(imgViewParams); 
     imgView.setImageResource(android.R.drawable.ic_menu_add); 
     imgViewParams.addRule(RelativeLayout.CENTER_IN_PARENT); 

     // Add child ImageView to parent RelativeLayout 
     relParent.addView(imgView); 

     // Set parent RelativeLayout to your screen 
     setContentView(relParent, relParentParam); 
    } 

} 
+0

這不回答問題。問題是關於創建一個自定義的'ViewGroup',供'WindowManager'使用。這不是關於創建一項活動。 – CommonsWare

+0

好的,但標題要求創建沒有XML的視圖 – Harsh4789

+0

答案需要回答問題,而不僅僅是與標題相關的郵政編碼。 – CommonsWare

相關問題