I have solved the problem but I'm not sure it's the best solution. I found
that my TextView objects were being added to the TableRow with a size of (0,
0). At first I tried calculating my text size using getPaint's
measureText method. This worked for the length but not height so I switched
over to getTextbounds() which gave me a height but it wasn't big enough to
get all of the text to display. By chance I figured out that if I added the
TextView objects using addView and feeding in -1 for both the height and the
width it would attach them with the appropriate size.
Example:
scoreRow.addView(scoreLabel, -1, -1);
scoreRow.addView(scoreText, -1, -1);
I haven't found using -1 for width and height documented anywhere. Can
anyone weigh in on weather this is good code or not? A better way to handle
my situation?
From: [email protected]
[mailto:[email protected]] On Behalf Of Giorgio Torres
Sent: Wednesday, August 03, 2011 6:33 PM
To: [email protected]
Subject: RE: [android-developers] Creating a Basic Layout
Is your goal to define a new Layout? If it is, you should extend the
LinearLayout, for example, and do whatever you want...
If you do not know how to define your layout using XML what makes you
believe that you can do it programmatically? Maybe is this what isn't
working. You should try using XML.
If those variables are necessary for the layout, you should try the "set"
methods.
Your code is really estrange to read, and I, with my experience, your code
could be expressed with the xml layout file without any loss.
--- Em qua, 3/8/11, John <[email protected]> escreveu:
De: John <[email protected]>
Assunto: RE: [android-developers] Creating a Basic Layout
Para: [email protected]
Data: Quarta-feira, 3 de Agosto de 2011, 23:19
In the program I'm trying to do this in I have my own custom object that
extends View. The constructor takes some programmatically set variables.
I'm not opposed to using XML but I had up to this point been controlling my
layout programmatically and wasn't sure how to get my custom object with the
variables required by the constructor in an XML layout.
From: [email protected]
[mailto:[email protected]] On Behalf Of Giorgio Torres
Sent: Wednesday, August 03, 2011 6:12 PM
To: [email protected]
Subject: Re: [android-developers] Creating a Basic Layout
The layout definition has to be programmatically?
--- Em qua, 3/8/11, John <[email protected]> escreveu:
De: John <[email protected]>
Assunto: [android-developers] Creating a Basic Layout
Para: [email protected]
Data: Quarta-feira, 3 de Agosto de 2011, 23:03
Can someone please explain to me why the below code does not display
anything? I am very confused.
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class LayoutTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mainLayout = new LinearLayout(this);
TableRow scoreRow = new TableRow(this);
TextView scoreLabel = new TextView(this);
TextView scoreText = new TextView(this);
TableLayout scoreTable = new TableLayout(this);
LinearLayout.LayoutParams mainLayoutParams = new
LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams rowLayout = new
LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams scoreLabelLayout = new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams scoreTextLayout = new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams scoreTableLayout = new
LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
mainLayout.setLayoutParams(mainLayoutParams);
scoreRow.setLayoutParams(rowLayout);
scoreLabel.setLayoutParams(scoreLabelLayout);
scoreText.setLayoutParams(scoreTextLayout);
scoreTable.setLayoutParams(scoreTableLayout);
scoreLabel.setText("Score: ");
scoreText.setText("0");
scoreRow.addView(scoreLabel);
scoreRow.addView(scoreText);
scoreTable.addView(scoreRow);
mainLayout.addView(scoreTable);
setContentView(mainLayout);
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en