Reputation: 4609
My activity layout has 3 main pieces: Title, picture, and an article. I am trying to force the title to be 15% of the screen, the picture to be 35% of the screen, and 50% for the article text. For some reason the article sometimes takes up much more space, and other times, its the picture. Is there a way to force dimensions?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Headline" />
<ImageView
android:id="@+id/picture"
android:layout_width="fill_parent"
android:layout_height ="wrap_content"
android:layout_weight = "35"/>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight = "50">
<TextView
android:id="@+id/storydata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Story Data" />
</ScrollView>
Upvotes: 0
Views: 1547
Reputation: 1
I had almost the same situation as you, and finally gave up on trying to use the xml layout from a resource file. No telling how many hours I spent fiddling with the xml, trying to get the percentages to line up correctly, with some success but not what I really wanted.
So I finally decided to just create the layout on my own, programatically, inside the main activity, and it turned out pretty well...
My layout is 3 parts. I have a company logo/image at the top, which I want to take up 20% of the screen.
In the middle section, I have a scrollarea with filenames inside it. The file list is scrollable both up and down, and left to right. I want this area to be 75% of the screen.
The 3rd section is just a single submit button which makes an asynchronous call to refresh the file list in the middle section, and it should be 5% of the screen.
So, here's the code...
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Resources res = this.getResources(); // Load the resources
// Get available screen size
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setId(topLayout);
layout.setBackgroundColor(0xff000000);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
layout.setLayoutParams(lp);
double logoHeight = screenHeight * .20;
logoHeight = Math.round(logoHeight);
Bitmap logoImg = BitmapFactory.decodeResource(res, R.drawable.standardlogo);
logoImg = Bitmap.createScaledBitmap(logoImg, screenWidth, (int)logoHeight, true);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(logoImg);
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.addView(imageView);
double bottomHeight = screenHeight * .05;
bottomHeight = Math.round(bottomHeight);
int scrollAreaHeight = screenHeight - (int)logoHeight - (int)bottomHeight - topHeight;
ScrollView scroll = new ScrollView(this);
scroll.setBackgroundColor(0xffd8d8d8);
LinearLayout.LayoutParams slp = new LinearLayout.LayoutParams(screenWidth, scrollAreaHeight);
scroll.setLayoutParams(slp);
layout.addView(scroll);
HorizontalScrollView hScroll = new HorizontalScrollView(this);
hScroll.setBackgroundColor(0xffd8d8d8);
LinearLayout.LayoutParams hlp = new LinearLayout.LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
hScroll.setLayoutParams(hlp);
scroll.addView(hScroll);
TextView tv = new TextView(this);
tv.setId(textArea);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lparams);
tv.setTypeface(Typeface.MONOSPACE);
tv.setText("");
hScroll.addView(tv);
Button btn = new Button(this);
btn.setId(sendButton);
btn.setOnClickListener(sendBtnListener);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("List Import Directory");
layout.addView(btn);
setContentView(layout);
}
It's quite a bit of code, but it does exactly what I want it to do, and it works on all screen sizes I've tested it on. I have just a single company logo png image for the app, which is a large image that's meant for a 10" tablet.
Note: The value of topHeight is hardcoded at 100, which works for all the devices I've tested it on. It's the height of the action or status bars at the top of the screen, and varies between devices, so I set it to 100 to handle up to two 48 pixel bars.. It leaves some unused space at the screen bottom if there's only one bar, but it's minimal.
Upvotes: 2