【问题标题】:Android textview border covering a big area of the map at the topAndroid textview边框覆盖了顶部地图的一大片区域
【发布时间】:2017-06-30 12:04:18
【问题描述】:

我有以下布局。我需要的是左手角的文本框,稍后我将插入一个计数器以显示地图将在几秒钟后刷新。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context="com.example.ns.appversion1.MapActivityFragment1"
    android:name="com.google.android.gms.maps.SupportMapFragment"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="96dip"
            android:orientation="vertical"
            android:weightSum="2" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="left"
                android:background="@drawable/border"
                android:text="Refreshing 1"
                android:textColor="#000"
                android:textSize="12sp" />


        </LinearLayout>
    </FrameLayout>
 </RelativeLayout>

这里是border.xml。现在的问题是,我只希望边框围绕单词,但在这里它与整个顶部区域接壤。我想限制在字里行间。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <stroke
        android:width="2dp"
        android:color="#0288D1" />
</shape>

【问题讨论】:

  • “我只想让边框围绕单词”是什么意思?您将其应用于 textView 而不是单词
  • 我想要这个词。但是当我将它应用到整个文本视图时,它看起来很丑
  • 然后扭曲视图或发布预期的输出
  • 我不明白你所说的扭曲视图是什么意思?
  • 你能附上一张你拥有什么以及你想要实现什么的图片吗?这样人们才能更好地帮助你

标签: android layout textview


【解决方案1】:

您的 TextView layout_width="match_parent" 将采用它可以采用的全部宽度,并且您正在将背景应用于视图而不是其中的文本,所以您所说的是正确的。

如果您只想在文本周围应用背景,请扭曲您的视图

   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:background="@drawable/border"
        android:text="Refreshing 1"
        android:textColor="#000"
        android:textSize="12sp" />

参考:What's the difference between fill_parent and wrap_content?

第二个问题:

  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#1bd4f6" /> <-- here
            <stroke
                android:width="2dp"
                android:color="#0288D1" />
        </shape>
    </item>

</layer-list>

【讨论】:

  • @charus 如果我希望 textview 的背景是白色或其他颜色并且周围有边框怎么办?
  • @Charus 目前我正在使用形状,所以我必须进行哪些更改才能使背景不透明而是特定颜色?
  • @charus 你的解决方案还可以,但是在 Refreshing 1 文本下方有一个问题,在框边框之前有相当大的空间
  • @user5313398 更新了答案希望你能接受;)
  • @charus 我看到有两个项目,请解释一下它们有什么不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
相关资源
最近更新 更多