EXTREMELY SIMPLE android layout
I know this HAS to be simple. It's making me crazy though. I've tried so much crap to get this looking good and NOTHING IS WORKING!! :/
Here is my problem:
I want a layout to look like this:
A fixed header with a height the same as an image that is put on the left. To the right of the image, I want just simple text that has the possibility of spanning 2 lines.
Below the fixed header, I need some sort of table layout like:
----------------------------------
Birth date | 09/23/1945
----------------------------------
Death date | 04/27/2011
----------------------------------
etc.
In any one of those rows, the text may span 2 lines depending on what is in the database. The text in the first column is all static while the text on the second column is dynamic. The table rows need to be put into a ScrollView (easy part) so it can hold a lot of information. I've tried using a TableL开发者_如何学JAVAayout and it has been giving me all sorts of headaches. Also for the fixed header, I used the whole RelativeLayout thing with layout_below and above to make that working but I can't get the image spacing and the text to the right correct. It's such a headache! Thanks for any help!
Well, I do not understand 100% how it's supposed to look, but remember that you can nest layouts, so you can use something like
<LinearLayout
orientation="vertical"
width="match_parent"
height="wrap_content">
<!-- header -->
<LinearLayout
orientation="horizontal"
width="match_parent"
height="wrap_content">
<ImageView
width="wrap_content"
height="wrap_content">
<TextView
width="match_parent"
height="wrap_content"
lines="2">
</LinearLayout>
<!-- Table here. You can use either TableLayout or nested LinearLayouts.
I prefer LinearLayouts. They have also a nice feature:
<LinearLayout>
<View width="match_parent" weight="1" />
<View width="match_parent" weight="1" />
</LinearLayout>
...makes both views equally wide, which you can use in your table. -->
</LinearLayout>
Maybe I can help more if you give a bit more information about problems you're encountering.
[EDIT] ...or as bigstones says in his comment - instead of table you can use ListView with a custom adapter, that's probably much better solution :)
Here's what I do.
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/left_text"
android:layout_width="75dip"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/right_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="2"
android:ellipsize="end" />
</LinearLayout>
Main App
public class Main extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<Datum> data = new ArrayList<Datum>();
data.add(new Datum("FIRST", "One line of text."));
data.add(new Datum("SECOND", "Two lines of text. Two lines of text. Two lines of text."));
data.add(new Datum("THIRD", "Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text."));
data.add(new Datum("FOURTH", "One line of text, again."));
ListView leftList = (ListView) findViewById(R.id.my_list);
leftList.setAdapter(new MyAdapter(this, R.layout.row, data));
}
}
class Datum
{
String left, right;
Datum(String left, String right)
{
this.left = left;
this.right = right;
}
}
class MyAdapter extends ArrayAdapter<Datum>
{
List<Datum> data;
int textViewResourceId;
MyAdapter(Context context, int textViewResourceId, List<Datum> data)
{
super(context, textViewResourceId, data);
this.data = data;
this.textViewResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(textViewResourceId, null);
}
Datum datum = data.get(position);
if (datum != null)
{
TextView leftView = (TextView) convertView.findViewById(R.id.left_text);
TextView rightView = (TextView) convertView.findViewById(R.id.right_text);
if (leftView != null)
{
leftView.setText(datum.left);
}
if (rightView != null)
{
rightView.setText(datum.right);
}
}
return convertView;
}
}
精彩评论