如何设置textview width wrap_content但限制为父宽度的1/3

时间:2015-05-04 14:44:58

标签: android textview android-layout-weight

拥有TextView,其宽度不应超过其父宽度的1/3。如果其宽度小于父级的1/3,则应具有wrap_content行为。它的横向兄弟将始终在它旁边开始。

尝试下面,它总是有1/3和2/3的硬切,所以如果text1的空间小于1/3,则TextView两个不会在它旁边开始。

LinearLayout更改为RelativeLayout,然后android:layout_weight="n"无效

基本上,需要定义宽度wrap_contentmaxWidth不超过1/3。

任何建议?

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_weight="2"
    />
</LinearLayout>

5 个答案:

答案 0 :(得分:3)

我认为每次更新textView时都必须动态检查父布局宽度,例如(我已经使用按钮测试了此代码并编辑了文本以更改textView - 工作没有问题):

<LinearLayout
    android:id="@+id/myLayout" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView 
        android:id="@+id/tvA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:id="@+id/tvB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
    />
</LinearLayout>

代码:

            TextView tvA = (TextView) findViewById(R.id.tvA);
            TextView tvB = (TextView) findViewById(R.id.tvB);
            LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);


            // code to use when the textView is updated
            // possibly button onClickListener?
            tvA.measure(0, 0);
            int textWidth = tvA.getMeasuredWidth();
            myLayout.measure(0,0);
            int layoutWidth = myLayout.getWidth();

            if (textWidth > (layoutWidth / 3)) {
                tvA.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
                tvB.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f));

            } else {
                tvA.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                tvB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            }

答案 1 :(得分:3)

  

基本上,需要定义宽度为wrap_content,而maxWidth不超过1/3。

如果这就是您所需要的,那么我的建议是取消权重方法并动态设置TextView的 <?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?> <!-- This atom/xml feed is an index to active advisories, watches and warnings issued by the National Weather Service. This index file is not the complete Common Alerting Protocol (CAP) alert message. To obtain the complete CAP alert, please follow the links for each entry in this index. Also note the CAP message uses a style sheet to convey the information in a human readable format. Please view the source of the CAP message to see the complete data set. Not all information in the CAP message is contained in this index of active alerts. --> <feed xmlns = 'http://www.w3.org/2005/Atom' xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1' xmlns:ha = 'http://www.alerting.net/namespace/index_1.0' > <!-- TZN = <CDT> --> <!-- TZO = <-5> --> <!-- http-date = Mon, 04 May 2015 01:30:00 GMT --> <id>https://alerts.weather.gov/cap/wwaatmget.php?x=NEC109&amp;y=0</id> <generator>NWS CAP Server</generator> <updated>2015-05-03T20:30:00-05:00</updated> <author> <name>w-nws.webmaster@noaa.gov</name> </author> <title>Current Watches, Warnings and Advisories for Lancaster (NEC109) Nebraska Issued by the National Weather Service</title> <link href='https://alerts.weather.gov/cap/wwaatmget.php?x=NEC109&amp;y=0'/> <entry> <id>...</id> <updated>2015-05-03T20:30:00-05:00</updated> <published>2015-05-03T20:30:00-05:00</published> <author>...</author> <title>...</title> <link href="http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e"/> <summary>...</summary> <cap:event>Flood Warning</cap:event> <cap:effective>2015-05-03T20:30:00-05:00</cap:effective> <cap:expires>2015-05-03T21:00:00-05:00</cap:expires> <cap:status>Actual</cap:status> <cap:msgType>Alert</cap:msgType> <cap:category>Met</cap:category> <cap:urgency>Immediate</cap:urgency> <cap:severity>Extreme</cap:severity> <cap:certainty>Observed</cap:certainty> <cap:areaDesc>Gage; Johnson; Lancaster</cap:areaDesc> <cap:polygon> 40.52,-96.79 40.66,-96.64 40.57,-96.46 40.52,-96.46 40.52,-96.37 40.46,-96.25 40.26,-96.48 40.52,-96.79 </cap:polygon> <cap:geocode> <valueName>FIPS6</valueName> <value>031067 031097 031109</value> <valueName>UGC</valueName> <value>NEC067 NEC097 NEC109</value> </cap:geocode> <cap:parameter> <valueName>VTEC</valueName> <value>/O.NEW.KOAX.TO.W.0002.150504T0130Z-150504T0200Z/</value> </cap:parameter> </entry> <entry> <id>http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e</id> <updated>2015-05-03T20:30:00-05:00</updated> <published>2015-05-03T20:30:00-05:00</published> <author> <name>w-nws.webmaster@noaa.gov</name> </author> <title>Tornado Warning issued May 03 at 8:30PM CDT until May 03 at 9:00PM CDT by NWS</title> <link href="http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e"/> <summary>THE NATIONAL WEATHER SERVICE IN OMAHA HAS ISSUED A * TORNADO WARNING FOR... NORTHWESTERN JOHNSON COUNTY IN SOUTHEASTERN NEBRASKA... NORTHEASTERN GAGE COUNTY IN SOUTHEASTERN NEBRASKA... SOUTHEASTERN LANCASTER COUNTY IN SOUTHEASTERN NEBRASKA... * UNTIL 900 PM CDT</summary> <cap:event>Tornado Warning</cap:event> <cap:effective>2015-05-03T20:30:00-05:00</cap:effective> <cap:expires>2015-05-03T21:00:00-05:00</cap:expires> <cap:status>Actual</cap:status> <cap:msgType>Alert</cap:msgType> <cap:category>Met</cap:category> <cap:urgency>Immediate</cap:urgency> <cap:severity>Extreme</cap:severity> <cap:certainty>Observed</cap:certainty> <cap:areaDesc>Gage; Johnson; Lancaster</cap:areaDesc> <cap:polygon>40.52,-96.79 40.66,-96.64 40.57,-96.46 40.52,-96.46 40.52,-96.37 40.46,-96.25 40.26,-96.48 40.52,-96.79</cap:polygon> <cap:geocode> <valueName>FIPS6</valueName> <value>031067 031097 031109</value> <valueName>UGC</valueName> <value>NEC067 NEC097 NEC109</value> </cap:geocode> <cap:parameter> <valueName>VTEC</valueName> <value>/O.NEW.KOAX.TO.W.0002.150504T0130Z-150504T0200Z/</value> </cap:parameter> </entry> <entry> <id>http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23430.SevereThunderstormWarning.1253A7A242A4NE.OAXSVROAX.d1cb461c9fb96a7e2ebbf6ddabdd25c7</id> <updated>2015-05-03T20:08:00-05:00</updated> <published>2015-05-03T20:08:00-05:00</published> <author> <name>w-nws.webmaster@noaa.gov</name> </author> <title>Severe Thunderstorm Warning issued May 03 at 8:08PM CDT until May 03 at 8:45PM CDT by NWS</title> <link href="http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23430.SevereThunderstormWarning.1253A7A242A4NE.OAXSVROAX.d1cb461c9fb96a7e2ebbf6ddabdd25c7"/> <summary>THE NATIONAL WEATHER SERVICE IN OMAHA HAS ISSUED A * SEVERE THUNDERSTORM WARNING FOR... SOUTHEASTERN LANCASTER COUNTY IN SOUTHEASTERN NEBRASKA... SOUTHWESTERN OTOE COUNTY IN SOUTHEASTERN NEBRASKA... * UNTIL 845 PM CDT * AT 808 PM CDT...A SEVERE THUNDERSTORM WAS LOCATED OVER PANAMA...OR</summary> <cap:event>Severe Thunderstorm Warning</cap:event> <cap:effective>2015-05-03T20:08:00-05:00</cap:effective> <cap:expires>2015-05-03T20:45:00-05:00</cap:expires> <cap:status>Actual</cap:status> <cap:msgType>Alert</cap:msgType> <cap:category>Met</cap:category> <cap:urgency>Immediate</cap:urgency> <cap:severity>Severe</cap:severity> <cap:certainty>Observed</cap:certainty> <cap:areaDesc>Lancaster; Otoe</cap:areaDesc> <cap:polygon>40.53,-96.6 40.67,-96.57 40.67,-96.32 40.52,-96.33 40.52,-96.56 40.53,-96.6</cap:polygon> <cap:geocode> <valueName>FIPS6</valueName> <value>031109 031131</value> <valueName>UGC</valueName> <value>NEC109 NEC131</value> </cap:geocode> <cap:parameter> <valueName>VTEC</valueName> <value>/O.NEW.KOAX.SV.W.0045.150504T0108Z-150504T0145Z/</value> </cap:parameter> </entry> </feed> 值。

这样的事情:

maxWidth

答案 2 :(得分:0)

解决方案很简单:你的第二个Textview的宽度必须像第一个那样是“0dp”。

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_weight="2"
    />
</LinearLayout>

答案 3 :(得分:0)

把它放在父母的1/3宽度的布局中怎么样?

<LinearLayout
  .. this is main parent
  android:weightSum="1" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

答案 4 :(得分:0)

通过引入ConstraintLayout可以像这样实现:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.333"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@+id/textView1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="initial text"/>
</android.support.constraint.ConstraintLayout>