Issue
EDIT: I think I figured it out. It was the background in the xml that was causing the difference.
Q:
I'm trying to add buttons to my app dynamically. (I need to create 1-6 buttons)
When I try to create a button in XML (to test the appearance), it looks like this:
Here's the xml I used to create that:
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/darker_gray"
android:text="add button"
/>
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/darker_gray"
android:text="add button"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
But when I try to create it programmatically, the buttons have a margin on the sides and they look a bit raised up.
How do I get then programmatic buttons to look the same as the XML buttons?
Is there some extra styling happening somewhere?
Dynamically created buttons (below XML buttons)
Here's the code I used to create the buttons dynamically:
var button = new Button(this);
var height = Utility.DpToPx(this, 100);
var width = ViewGroup.LayoutParams.MatchParent;
var layoutParameters = new LinearLayout.LayoutParams(width, height);
button.LayoutParameters = layoutParameters;
button.Text = "Test";
EDIT: I think I figured it out. It's the background that I put in the xml that is causing the different effect:
android:background="@android:color/darker_gray"
Solution
My confusion was caused by the fact that I did not add a background color to the buttons that I created programatically. That is why the appearances were different.
Answered By - Simon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.