开发者

How to fix TextView on Java, Android

开发者 https://www.devze.com 2023-04-06 19:18 出处:网络
I have the layout: <?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayout android:orientation=\"horizontal\"

I have the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weightSum="1">
    <LinearLayout 
        android:layout_weight="0.2"
        android:orientation="horizontal"
        android:layout_height="fill_parent" 
        android:layout_width="wrap_content" 
        android:id="@+id/linearLayout1">
        <TextView 
            android:text="TextView" 
            android:gravity="center"
            android:layout_gravity="center"
            android:id="@+id/textView2"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" />
    </LinearLayout>
    <LinearLayout 
        android:layout_weight="0.8"
        android:background="@android:color/white"
        android:orientation="horizontal"
        android:layout_height="fill_parent" 
        android:layout_width="wrap_content" 
        android:id="@+id/linearLayout2">
        <TextView 
            android:layout_gravity="cent开发者_开发知识库er"
            android:gravity="center"
            android:text="123" 
            android:id="@+id/textView1" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" />
    </LinearLayout>
</LinearLayout>

But I have a problem: If I put in TextView2 long text than TextView2 will be more longer default size and TextView1 will be more shortly default size. But I need than textview2 and textview1 have fixed size on width and will increase on height only. How can I do that? Thank you


Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
      <TextView 
        android:text="TextView" 
        android:gravity="center"
        android:layout_gravity="center"
        android:id="@+id/textView2"
        android:layout_width="0dp" 
        android:layout_height="fill_parent"
        android:layout_weight="1"/>
      <TextView 
        android:layout_gravity="center"
        android:gravity="center"
        android:text="123" 
        android:id="@+id/textView1" 
        android:layout_width="0dp" 
        android:layout_height="fill_parent" 
    android:layout_weight="1"/>
</LinearLayout>


use these in both TextView..size depend on you what you want..i have give 50dp

android:maxWidth="50dp"
android:minWidth="50dp"


You saying TextView width as fixed size: then you can use

android:layout_width="150dp" or something.., it makes your TextView width fixed and use android:ellipsize="end" to fix it.

and android:layout_height="wrap_content"

Hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消