【问题标题】:android layout with center text and two views around带有中心文本和两个视图的android布局
【发布时间】:2014-08-05 08:24:08
【问题描述】:

我有这样的布局来显示一些标题:

但是当居中的文本很长时,视图不好。 这就是发生的事情:

如何获得这样的东西:

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

<View
    android:background="#0000CC"
    android:layout_width="fill_parent"
    android:layout_height="4dp"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/headerTitle"
    android:layout_margin="4dp"
    android:visibility="invisible"
    android:layout_centerVertical="true" />

<TextView
    android:id="@+id/headerTitle"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:gravity="center"
    android:text="The quick brown fox jumps over the lazy dog"
    android:textSize="20sp"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    android:layout_centerInParent="true" />

<View
    android:background="#CC0000"
    android:layout_width="fill_parent"
    android:layout_height="4dp"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/headerTitle"
    android:layout_margin="4dp"
    android:layout_centerVertical="true" />

感谢您的帮助

UPD:最接近的解决方案

我对项目使用 LinearLayout + layout_width="0dp" + layout_weight=0.15/0.7/0.15。

【问题讨论】:

  • 非常简单.. 两边都加内边距。
  • 填充没有帮助。
  • android:layout_centerVertical="true" 更改为 android:layout_gravity="center_vertical" 在您的 &lt;View ... 标签中,并从 &lt;TextView ... 标签中删除 android:layout_centerInParent="true"。不要忘记清除您的项目。
  • layout_gravity 也解决不了问题

标签: android android-layout android-relativelayout


【解决方案1】:

从此链接获取答案

Justify text in an Android app using a WebView but presenting a TextView-like interface?

这让我很紧张,我承认。我喜欢 TextViews 在代码中看起来像 TextViews,即使我使用 WebView 作为实现 text-align:justified 格式的手段,我也不想看它方式。

我创建了一个自定义视图(丑陋,可能很糟糕),它实现了我在TextView 中常用的方法,并修改了 WebView 的内容以反映这些变化。 无论它对其他人有用还是我真的不知道的潜在危险,对我来说它有效,我已经在几个项目中使用它并且没有遇到任何问题。唯一的小不便是,我认为它对记忆的影响更大,但如果它只是一两个(如果我错了,请纠正我)。

结果如下:

而以编程方式设置它的代码就这么简单:

 JustifiedTextView J = new JustifiedTextView();
                   J.setText("insert your text here");

当然,这样离开它是愚蠢的,所以我还添加了更改字体大小和字体颜色的方法,这些方法基本上都是我使用 TextViews 的目的。这意味着我可以这样做:

 JustifiedTextView J = new JustifiedTextView();
                   J.setText("insert your text here");
                   J.setTextColor(Color.RED);
                   J.setTextSize(30);

并获得以下结果(图像被裁剪):

但是,这并不是要向我们展示它的外观,而是要分享你是如何做到的!

我知道,我知道。 这是完整的代码。它还解决了设置透明背景和将 UTF-8 字符串加载到视图中时出现的问题。有关详细信息,请参阅 reloadData() 中的 cmets。

public class JustifiedTextView extends WebView{
    private String core      = "<html><body style='text-align:justify;color:rgba(%s);font-size:%dpx;margin: 0px 0px 0px 0px;'>%s</body></html>";
    private String textColor = "0,0,0,255";
    private String text      = "";
    private int textSize     = 12;
    private int backgroundColor=Color.TRANSPARENT;

    public JustifiedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setWebChromeClient(new WebChromeClient(){});
    }

    public void setText(String s){
        this.text = s;
        reloadData();
    }

    @SuppressLint("NewApi")
    private void reloadData(){

        // loadData(...) has a bug showing utf-8 correctly. That's why we need to set it first.
        this.getSettings().setDefaultTextEncodingName("utf-8");

        this.loadData(String.format(core,textColor,textSize,text), "text/html","utf-8");

        // set WebView's background color *after* data was loaded.
        super.setBackgroundColor(backgroundColor);

        // Hardware rendering breaks background color to work as expected.
        // Need to use software renderer in that case.
        if(android.os.Build.VERSION.SDK_INT >= 11)
            this.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
    }

    public void setTextColor(int hex){
        String h = Integer.toHexString(hex);
        int a = Integer.parseInt(h.substring(0, 2),16);
        int r = Integer.parseInt(h.substring(2, 4),16);
        int g = Integer.parseInt(h.substring(4, 6),16);
        int b = Integer.parseInt(h.substring(6, 8),16);
        textColor = String.format("%d,%d,%d,%d", r, g, b, a); 
        reloadData();
    }

    public void setBackgroundColor(int hex){
        backgroundColor = hex;
        reloadData();
    }

    public void setTextSize(int textSize){
        this.textSize = textSize;
        reloadData();
    }
}

【讨论】:

    【解决方案2】:

    尝试像这样使用 LinearLayout。希望对你有帮助。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="horizontal" >
    
    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#ffffff" />
    
    <TextView
        android:id="@+id/headerTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.5"
        android:gravity="center"
        android:text="The quick brown fox jumps over the lazy dog"
        android:textColor="#ffffff"
        android:textSize="20sp" />
    
    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_centerVertical="true"
        android:layout_margin="4dp"
        android:layout_toRightOf="@+id/headerTitle"
        android:layout_weight="1"
        android:background="#ffffff" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:layout_gravity="center_vertical"
      android:orientation="horizontal">
      
      <View
          android:background="#0000CC"
          android:layout_width="20dp"
          android:layout_weight="1"
          android:layout_gravity="center"
          android:layout_height="4dp"
          android:layout_margin="4dp" />
      
      <TextView
          android:id="@+id/headerTitle"
          android:layout_width="150dp"
          android:layout_height="wrap_content"
          android:layout_marginBottom="6dp"
          android:layout_marginLeft="50dp"
          android:layout_marginRight="50dp"
          android:layout_marginTop="6dp"
          android:gravity="center"
          android:text="The quick "
          android:textSize="20sp" />
      
      <View
          android:background="#CC0000"
          android:layout_width="20dp"
          android:layout_weight="1"
          android:layout_gravity="center"
          android:layout_height="4dp"
          android:layout_margin="4dp" />
      
      </LinearLayout>
      

      试试这个

      【讨论】:

        猜你喜欢
        • 2013-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多