2013-05-18 148 views
1

我正在开发一个实现简单指南针的应用程序。设置内容视图层次结构

我在扩展ImageView的类(Rose.java)中创建和操作指南针本身。

在我的主要活动我想玫瑰对象添加到布局,我有这样的代码

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

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 

    rose = new Rose (this); 


    setContentView(rose); 
    } 

的事情是,你也知道,通过使用的setContentView的“玫瑰”将是全屏和布局的其他元素将不会显示。

我特别要求一个替代方法,将玫瑰对象添加到布局而不忽略其其他元素。

在此先感谢。

回答

1

你可以在xml中分配LinearLayout的宽度和高度。

在xml文件中定义一个LinearLayout,并定义您选择的宽度和高度。将布局放在你喜欢的地方。自定义视图的内容添加到的LinearLayout

setContentView(R.id.activity_main); 
    LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout); 
    rose = new Rose(this); 
    ll.addView(rose); 

例子:

actiivty_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 
    // Other views like textview's and button above linear layout 

    <LinearLayout 
     android:layout_width="40dp" // mention the width of you choice 
     android:layout_height="40dp" // // mention the height of you choice 
     android:layout_above="@+id/button1" 
     android:layout_alignParentRight="true" 
     android:id="@+id/linearlayout" 
     > 
    </LinearLayout> 
     // other views below linear layout 
</RelativeLayout> 
+0

感谢,它的工作! – Mike

+0

@mike很高兴帮助。请点击勾号接受答案。这将有助于其他访问该帖子的人。 – Raghunandan