Reputation: 1996
I am working on an android app in which I am seeing some disparity between what is showing up on the actual device and what is showing up when I run the app on the emulator.
Here is the relevant code.
I have an xml file which defines a shape - a simple blue rectangle:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:radius="4dp" />
<gradient
android:angle="270"
android:startColor="#449def"
android:endColor="#2f6699" />
<stroke
android:width="1dp"
android:color="#2f6699" />
</shape>
I then have another "view" xml file which uses that shape, shown here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:background="@drawable/blue_rectangle"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Finally, I have a custom ViewGroup which extends RelativeLayout. This custom layout has a child view object of the view which I described above. The custom layout also overrides the onLayout method such that it handles the layout process itself. That code is here:
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4)
{
//Layout the children of this viewgroup
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++)
{
NodeView view = (NodeView) this.getChildAt(i);
view.layout(40, 40, 40, 40); //Hard code the child object position
}
}
As you can see, I am hardcoding the child views to have position (40, 40) and have width and height of 40 dip.
Unfortunately, I am getting varying results. This is what it looks like on the Android emulator (part of the Android development tools):
This is what it looks like on my actual Android device (and this is what I want it to look like as well):
What is causing the disparity in size of the blue box? Any ideas? Thanks!
Upvotes: 2
Views: 406
Reputation: 3005
The emulator has a setting when you make your AVD called Abstracted LCD Density. Make sure that is set to the same as the device. See if that works.
Upvotes: 3